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 > Selecting sequence inside row copy query.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-09-09, 01:09
asp_crazy_guy asp_crazy_guy is offline
Registered User
 
Join Date: Mar 2009
Posts: 73
Selecting sequence inside row copy query.

Hello folks,

I have a scenario where I have to duplicate insert a record inside the same table. I am having problem in generating the serial for the insert query as the id field in the table is a sequence field.
Here is my query
Code:
INSERT INTO ADMINISTRATOR.UWPROPTRTY (GENTREATYSRNO, GENTREATYYEAR, GENTREATYCODE, GENSUBBUSICLASS, GENCLIENTCODE, GENCURRCODE, GENENTRYDATE, GENTREATYDATEFROM, GENTREATYDATETO, GENDESCRIPTION, GENPTRTYSTATUS, DATETIME, LASTMODIFYBY, TERMINALID, GENRENEWSTAT, PREVTRTSRNO)
 values (select (SELECT NEXTVAL FOR administrator.ptrt_seq as seqid FROM sysibm.sysdummy1), GENTREATYYEAR, GENTREATYCODE, GENSUBBUSICLASS, GENCLIENTCODE, GENCURRCODE, GENENTRYDATE, GENTREATYDATEFROM, GENTREATYDATETO, GENDESCRIPTION, GENPTRTYSTATUS, DATETIME, LASTMODIFYBY, TERMINALID, GENRENEWSTAT, PREVTRTSRNO from administrator.uwproptrty where gentreatysrno=261 )
Help me guys
Best regards, Mike.
Reply With Quote
  #2 (permalink)  
Old 09-09-09, 01:19
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
What is your problem?
Reply With Quote
  #3 (permalink)  
Old 09-09-09, 01:19
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
I am not sure I understand what you are trying to do, but if want to find out what value was used in an insert with a NEXTVAL FOR sequence-name, then you should look at the FINAL TABLE syntax.

Select GENTREATYSRNO from FINAL TABLE (
INSERT INTO ADMINISTRATOR.UWPROPTRTY
(GENTREATYSRNO, GENTREATYYEAR, GENTREATYCODE, GENSUBBUSICLASS, GENCLIENTCODE, GENCURRCODE, GENENTRYDATE, GENTREATYDATEFROM, GENTREATYDATETO, GENDESCRIPTION, GENPTRTYSTATUS, DATETIME, LASTMODIFYBY, TERMINALID, GENRENEWSTAT, PREVTRTSRNO)
values (NEXTVAL FOR administrator.ptrt_seq, GENTREATYYEAR, GENTREATYCODE, GENSUBBUSICLASS, GENCLIENTCODE, GENCURRCODE, GENENTRYDATE, GENTREATYDATEFROM, GENTREATYDATETO, GENDESCRIPTION, GENPTRTYSTATUS, DATETIME, LASTMODIFYBY, TERMINALID, GENRENEWSTAT, PREVTRTSRNO)
);
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #4 (permalink)  
Old 09-09-09, 01:26
asp_crazy_guy asp_crazy_guy is offline
Registered User
 
Join Date: Mar 2009
Posts: 73
I cannot select the next sequence as the primary key for the row I am inserting inside my select statement. Because select next val for seq)as id is not working with multiple select.

Quote:
Originally Posted by tonkuma
What is your problem?
Reply With Quote
  #5 (permalink)  
Old 09-09-09, 01:28
asp_crazy_guy asp_crazy_guy is offline
Registered User
 
Join Date: Mar 2009
Posts: 73
Thanks for the reply Marcus. I am trying to insert a row again in the same table with a new serial. I have limitation in my table that the primary key is a sequence field so that's why I am trying to select nextval from the sequence so I could fetch the primary key to insert for the new row.

Quote:
Originally Posted by Marcus_A
I am not sure I understand what you are trying to do, but if want to find out what value was used in an insert with a NEXTVAL FOR sequence-name, then you should look at the FINAL TABLE syntax.

Select GENTREATYSRNO from FINAL TABLE (
INSERT INTO ADMINISTRATOR.UWPROPTRTY
(GENTREATYSRNO, GENTREATYYEAR, GENTREATYCODE, GENSUBBUSICLASS, GENCLIENTCODE, GENCURRCODE, GENENTRYDATE, GENTREATYDATEFROM, GENTREATYDATETO, GENDESCRIPTION, GENPTRTYSTATUS, DATETIME, LASTMODIFYBY, TERMINALID, GENRENEWSTAT, PREVTRTSRNO)
values (NEXTVAL FOR administrator.ptrt_seq, GENTREATYYEAR, GENTREATYCODE, GENSUBBUSICLASS, GENCLIENTCODE, GENCURRCODE, GENENTRYDATE, GENTREATYDATEFROM, GENTREATYDATETO, GENDESCRIPTION, GENPTRTYSTATUS, DATETIME, LASTMODIFYBY, TERMINALID, GENRENEWSTAT, PREVTRTSRNO)
);
Reply With Quote
  #6 (permalink)  
Old 09-09-09, 07:46
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
The syntax of your INSERT is incorrect. Check the manual.
Reply With Quote
  #7 (permalink)  
Old 09-09-09, 23:08
asp_crazy_guy asp_crazy_guy is offline
Registered User
 
Join Date: Mar 2009
Posts: 73
Hi..

Hello thanks for the replies,

The "values" in insert statement was creating problems. After I removed it its working fine now . Thanks to you folks I am saved from my boss once again
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