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

Ken Burkhardt picture Ken Burkhardt · Feb 25, 2010 · Viewed 33.8k times · Source

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?

Answer

Brian picture Brian · Dec 15, 2010

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...