-2

How do I convert this code from T-SQL into DB2 LUW, it seems so easy with T-SQL but in DB2 can't find any solution. See code below:

DECLARE @sqlCommand varchar(1000)
DECLARE @columnList varchar(75)
DECLARE @city varchar(75)

SET @columnList = 'AddressID, AddressLine1, City'
SET @city = '''London'''
SET @sqlCommand = 'SELECT ' + @columnList + ' FROM Person.Address WHERE City = ' + @city

EXEC (@sqlCommand)
Mark Barinstein
  • 11,456
  • 2
  • 8
  • 16
GeekyDad
  • 75
  • 1
  • 7

2 Answers2

0

The problem is that you can’t ‘select to nowhere’ in a compound statement in DB2. Db2 CLP can return you the result set of a single sql statement, but it doesn’t try to do the same for select statements in a compound statement. If you want to print the result set from a select statement in a compound statement, you can, for example, declare a cursor, fetch it in a loop, and use dbms_output.put_line calls to print the values of variables.

Mark Barinstein
  • 11,456
  • 2
  • 8
  • 16
0

Not Pretty but you can find an example of the bottom of this page: Stored Procedures and Dynamic SQL Returning a Result set

Essentially you most:

1) create a dynamic SQL string 2) prepare the string into a statement 3) Link the statement to a cursor you're going to declare as WITH RETURN

Opening the cursor will be the last line in your procedure.

Jim Castro
  • 864
  • 5
  • 10