views:

358

answers:

2

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?

A: 

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.

robertnl
Hmmmmm... not the answer I was hoping for! :-("Is there someone else up there we could talk to?" (Monty Python and the Holy Grail (1975)
KenB
If the going gets though, the though get going ... (but if "ad hoc sql + variables" is the thing you need, you should really try Rexx. Don't forget that it's interpreted. So, even with embedded sql: edit your source, save and run. It works like a charm)
robertnl
A: 

Here are a couple of other alternatives.

Data Transfer Tool - You can run the iSeries Data Transfer Tool from the command line (RTOPCB). First, run the GUI version and create a definition file. If you edit this file with a text editor, you will see that this is just an old-fashioned INI file and you can easily find the line with the query in it. From there, you could write a batch file or otherwise pre-process the text file to allow you to manipulate the query before submitting it to the query tool.

QSHELL - If you can log on to the iSeries interactively, then you may find the QSHELL environment more familiar than CL or REXX (although REXX is kind of fun). QSHELL is a full POSIX environment running on the iSeries. Use the command STRQSH to start QSHELL. You can have ksh or csh as a shell. Inside QSHELL, there is a command called "db2" that submits queries. So, you should be able to do something like this inside QSHELL:

system> VariableName = 'blah blah'
system> db2 "select * from TableName where Column = \'$VariableName\'"

You may have to fiddle with the quotes to get ksh to pass them correctly.

Also, inside QSHELL, you should have a full Perl installation that will allow you to use DBI to get data.

Some other ways to interact with data on the iSeries: query from the client with Python via ODBC; query from the client with Jython via JDBC; install Jython directly on the iSeries and then query via JDBC.

Tim