I have been researching this for a few days now and can not specifically find an answer to my issue. I want to be able to dynamically create sql statements to run against my DB2 database. Below is some test code I have playing with to see how the DB2 interprets my sql, but I get an error every time I run this. Please help.
Begin
Declare Monthcol integer;
Declare TXPage integer;
Declare TXYear integer;
Declare text varchar(2000);
set Monthcol = 11;
set TXPage = 10190;
set TXYear = 2018;
set text = 'Select GLMN'|| Monthcol || 'from gldbfa.glpgl where glyear =
2018 and glpage = 10190';
Print text;
end;
I have tried casting the variables to varchar and I have tried moving the print section to after the End. I get
"SQL State: 42601 Vendor Code: -104 Message: [SQL0104] Token TEXT was not valid. Valid tokens: :. Cause . . . . . : A syntax error was detected at token TEXT. Token TEXT is not a valid token."
if print is before end.
I get
"SQL State: 42601 Vendor Code: -104 Message: [SQL0104] Token PRINT was not valid. Valid tokens: ( CL END GET SET CALL DROP FREE HOLD LOCK OPEN WITH ALTER. Cause . . . . . : A syntax error was detected at token PRINT."
if print is after the End.
This is super easy to do in SQL Server, but I am very new to DB2. Thank you.
;. Are you using any particular tool to run your SQL statements? IBM Data Studio will syntax check locally, which can be a good help when struggling with syntax - Paul Vernon