If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Insert results from stored procedure into another table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-11-11, 16:57
Comrade Comrade is offline
Registered User
 
Join Date: Nov 2011
Posts: 5
Insert results from stored procedure into another table

Hello,

I have a stored procedure which returns some data upon execution. How do I insert the data into another table (not temporary)? I have appropriately created the table in which the data is to be inserted.

There are a few threads on the topic, but some of them are related to temporary table and I don't follow the discussion very well.
Reply With Quote
  #2 (permalink)  
Old 11-14-11, 09:35
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You will have to do it with another Stored Procedure or at the application level. There is no direct way to do it with a single SQL statement.

Andy
Reply With Quote
  #3 (permalink)  
Old 11-14-11, 14:51
Comrade Comrade is offline
Registered User
 
Join Date: Nov 2011
Posts: 5
Thanks, Andy. I think I'll write a stored procedure then. Can you point me to some direction? Can I just use select and insert statements to achieve what I need?
Reply With Quote
  #4 (permalink)  
Old 11-14-11, 14:57
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You will have to loop through the result set of the stored procedure like this:

Code:
         call MySchema.MySP(z_Parm1,z_Parm2,z_Parm2);
         ASSOCIATE RESULT SET LOCATOR (local_RS_Locator) with procedure MySchema.MySP;
         ALLOCATE Local_Cursor CURSOR for result set local_RS_Locator;
         SET ENDTABLE = 0;
         open Local_Cursor;
         fetch Local_cursor into col1,col2,col3;
         WHILE (endtable = 0) do
            insert into MySchema,MyTable values (col1,Col1,col3);

            fetch Local_cursor into col1,col2,col3;
         end while;

Andy
Reply With Quote
Reply

Tags
db2, insert, result set, stored proc

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On