2

is there an easy way to display/print a variable on the output of the IBM i ACS Run SQL Script screen?

For having a quick check when writing a script it would very helpful to be able to display the content of a variable. E.g. in below script, I want to know the value of "my_counter".

BEGIN

DECLARE my_counter INT;

SET my_counter = my_counter + (SELECT count(*) FROM QSYS2.OBJECT_LOCK_INFO WHERE SYSTEM_OBJECT_SCHEMA = 'ABC' AND SYSTEM_OBJECT_NAME = 'DEF' AND OBJECT_TYPE = '*FILE');
SET my_counter = my_counter + (SELECT count(*) FROM QSYS2.OBJECT_LOCK_INFO WHERE SYSTEM_OBJECT_SCHEMA = 'ABC' AND SYSTEM_OBJECT_NAME = 'XYZ' AND OBJECT_TYPE = '*FILE');

PRINT my_counter; --> I want to print the content of variable my_counter but PRINT is not a valid keyword (all other valid keywords have a blue color, this one does not).

END;

You probably also notice that I'm doing "my_counter = my_counter + ...", I tried using "my_counter += ..." but no such thing. Is there a better way of doing this?

KoenS
  • 39
  • 3

2 Answers2

1

Hellow KoenS,

This is how I do this:

create or replace variable @my_Counter integer default 0;

SET @my_counter = @my_counter + (SELECT count(*) FROM QSYS2.OBJECT_LOCK_INFO WHERE SYSTEM_OBJECT_SCHEMA = 'ABC' AND SYSTEM_OBJECT_NAME = 'ABC' AND OBJECT_TYPE = '*FILE');
SET @my_counter = @my_counter + (SELECT count(*) FROM QSYS2.OBJECT_LOCK_INFO WHERE SYSTEM_OBJECT_SCHEMA = 'XYZ' AND SYSTEM_OBJECT_NAME = 'XYZ' AND OBJECT_TYPE = '*FILE');

select @my_Counter from sysibm.sysdummy1;

drop variable @my_Counter;

Variables are CL SRVPGM that are located in a library, if you have problems with it, especify library (abc.@my_Counter or use SET SCHEMA and SET PATH comands before create and use the variable.

I use @ before variables by convention.

To display a variable use sysibm.sysdummy1 file.

0

To print the contents of a variable, one could also use

values @my_Counter;
Steve B
  • 21
  • 3