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 from old table not working

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-26-10, 16:05
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
insert from old table not working

insert into table_history (
select * from old table (
delete from table_main where carton_no= '100'
)
)

gives error SQL20165N. Both tables have same structure. I am trying move data from table_main to table_history

insert into table_history
select * table_main where carton_no= '100';

delete from table_main where carton_no= '100';
works fine

Any suggestions . Ver DB2 v9.5

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 10-26-10, 17:33
Rajesh1203 Rajesh1203 is offline
Registered User
 
Join Date: Aug 2010
Posts: 38
Quote:
Originally Posted by phil72 View Post
insert into table_history (
select * from old table (
delete from table_main where carton_no= '100'
)
)

gives error SQL20165N. Both tables have same structure. I am trying move data from table_main to table_history

insert into table_history
select * table_main where carton_no= '100';

delete from table_main where carton_no= '100';
works fine

Any suggestions . Ver DB2 v9.5

Thanks in advance

do it in two different queries:
Insert into table_history select * table_main where carton_no= '100';

delete from table_main where carton_no= '100';
Reply With Quote
  #3 (permalink)  
Old 10-26-10, 20:34
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Please try:

Code:
WITH
 delete_op AS (
SELECT *
  FROM OLD TABLE
       (DELETE FROM table_main
         WHERE carton_no = '100'
       )
)
SELECT COUNT(*) insert_count
  FROM FINAL TABLE
       (INSERT INTO table_history
        SELECT * FROM delete_op
       )
;
Reply With Quote
  #4 (permalink)  
Old 10-27-10, 14:03
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
Thanks
tonkuma
this code works fine in control center but i get SQLSTATE: 42601 when i try to deploy in procedure . i tried to assign count to varriable also

set temp = (WITH
delete_op AS (
SELECT *
FROM OLD TABLE
(DELETE FROM table_main
WHERE carton_no = '100'
)
)
SELECT COUNT(*) insert_count
FROM FINAL TABLE
(INSERT INTO table_history
SELECT * FROM delete_op
)
);
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