Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DB2 - How to run an ad hoc select query with a parameter in IBM System i Access for Windows GUI Tool

I would like to run some ad hoc select statements in the IBM System I Navigator tool for DB2 using a variable that I declare.

For example, in the SQL Server world I would easily do this in the SQL Server Management Studio query window like so:

DECLARE @VariableName varchar(50);
SET @VariableName = 'blah blah';

select * from TableName where Column = @VariableName;

How can I do something similar in the IBM System I Navigator tool?

like image 290
Ken Burkhardt Avatar asked Feb 25 '10 19:02

Ken Burkhardt


2 Answers

I ran across this post while searching for the same question. My coworker provided the answer. It is indeed possible to declare variables in an ad hoc SQL statement in Navigator. This is how it is done:

CREATE OR REPLACE VARIABLE variableName VARCHAR(50);
SET variableName = 'blah';
SELECT * FROM table WHERE column = variableName;
DROP VARIABLE variableName;

If you don't drop the variable name it will hang around until who knows when...

like image 72
Brian Avatar answered Sep 30 '22 13:09

Brian


At the moment, we're working on the same issue at work. Unfortunaly, we concluded that this is not possible. I agree, it would be great but it just doesn't work that way. iNavigator doesn't support SET or Define. You can do that in embedded SQL but this is not embedded SQL. Even if you create a separate document (xxx.sql), then need to open this document to run the script what makes it an interactive script (that is, DECLARE SECTION is not allowed).

As an alternative, in the SQL screen/script you can use CL:. Anything after this prefix is executed as CL command. You may manipulate your tables (e.g. RNMF) this way.

As a second alternative, the iSeries does support Rexx scripts (default installed with the os). Rexx is good dynamic script language and it does support embedded SQL. I've done that a lot of times and it works great. I even created scripts for our production environment.

Just create one 'default' script with an example PREPARE and CURSOR statement and copy at will. With that script you can play around. See the Rexx manual for the correct syntax of exec-sql. Also, you do have STDIN and STDOUT but you can use 'OVRDBF' to point to a database table (physical file). Just let me know if you need an example Rexx script.

Notice that the manual "SQL embedded programming" does have Rexx examples.

like image 37
robertnl Avatar answered Sep 30 '22 14:09

robertnl