Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 3rd, 2008, 04:45 PM
Justin
Guest
 
Posts: n/a
Default Dynamic sql statement called in a stored procedure SP


We have a stored procedure that dynamically builds a sql statement.

Is there an example of how to get the SP to return the result set of a
dynamically generated sql statement.

here is an oversimplified sql statement - just to prove the point:

set SQLStatement = 'select c1, c2 from syscat.tables ';
set SQLStatement = SQLStatement || 'where other stuff...'
set SQLStatement = SQLStatement || 'UNION select A2, B3 from X where
other stuff...'


--execute immediate SQLStatement;--
PREPARE stmt1 from SQLStatement;
EXECUTE stmt1;
  #2  
Old October 3rd, 2008, 07:35 PM
jefftyzzer
Guest
 
Posts: n/a
Default Re: Dynamic sql statement called in a stored procedure SP

On Oct 3, 8:38*am, Justin <kfw...@hotmail.comwrote:
Quote:
We have a stored procedure that dynamically builds a sql statement.
>
Is there an example of how to get the SP to return the result set of a
dynamically generated sql statement.
>
here is an oversimplified sql statement - just to prove the point:
>
set SQLStatement = 'select c1, c2 from syscat.tables ';
set SQLStatement = SQLStatement || *'where other stuff...'
set SQLStatement = SQLStatement || *'UNION select A2, B3 from X where
other stuff...'
>
--execute immediate SQLStatement;--
PREPARE stmt1 from SQLStatement;
EXECUTE stmt1;
CREATE PROCEDURE RET_TEST()
LANGUAGE SQL
SPECIFIC ARCHIVE_RET_TEST
INHERIT SPECIAL REGISTERS
DYNAMIC RESULT SETS 1
BEGIN
DECLARE V_RETURN CHAR(30);--

DECLARE C_RETURN CURSOR WITH RETURN FOR S_RETURN;--

-- Build statement here
SET V_RETURN = 'SELECT * FROM SYSIBM.SYSDUMMY1';--

PREPARE S_RETURN FROM V_RETURN;--

OPEN C_RETURN;--
END;


--Jeff
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles