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 of recursive query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-04-09, 11:45
NMadson NMadson is offline
Registered User
 
Join Date: Apr 2009
Location: San Francisco
Posts: 4
Question Insert results of recursive query

I'm trying to save the results of a recursive query into a table.

my recustive table = temptab

INSERT INTO MYSCHEMA.MY_TABLE
SELECT * FROM TEMPTAB

I get an error message "unexpected token: insert".

I also tried creating a view and creating a table based on the select statement; all give me the same message about an "unexpected token."

Any ideas?

Nolan
Reply With Quote
  #2 (permalink)  
Old 04-04-09, 12:14
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Please try:
INSERT INTO MYSCHEMA.MY_TABLE WITH temptab(...) AS(...) SELECT * FROM temptab;
Reply With Quote
  #3 (permalink)  
Old 04-04-09, 12:19
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
An example:
Code:
------------------------------ Commands Entered ------------------------------
CREATE TABLE my_table
(id INTEGER);
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
INSERT INTO my_table
WITH temptab(id) AS (
VALUES 1
UNION ALL
SELECT id + 1
  FROM temptab
 WHERE id < 5
)
SELECT * FROM temptab
;
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
SELECT * FROM my_table;
------------------------------------------------------------------------------

ID         
-----------
          1
          2
          3
          4
          5

  5 record(s) selected.
Reply With Quote
  #4 (permalink)  
Old 04-04-09, 12:59
NMadson NMadson is offline
Registered User
 
Join Date: Apr 2009
Location: San Francisco
Posts: 4
thx

that did it
N
Reply With Quote
Reply

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