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 into Select proble,

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-06-09, 12:16
makhan1983 makhan1983 is offline
Registered User
 
Join Date: Aug 2009
Posts: 1
Question Insert into Select proble,

Hi guys,
I am trying to insert records from a table. The records are for year 2009 and i need to copy the same records with year 2010 in it. THe catch here is the primary key column should be incremented from the max value from 2009. I wrote this query but I get an error.
The table has 3 colums app_id, year and timestamp

INSERT INTO SYS_APP
SELECT MAX(APP_ID)+1 FROM SYS_APP WHERE YEAR IN ('2009','2010')
SELECT '2010'
,CURRENT TIMESTAMP
FROM SYS_APP WHERE YEAR = '2009'

Please help.
Reply With Quote
  #2 (permalink)  
Old 08-06-09, 13:58
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
I think the best option would be to alter your APP_ID column to make it an IDENTITY, generated by default. Barring that, you could also try something like this:
Code:
insert into sys_app (app_id, year, ts) 
select m+s, '2010', current_timestamp from (
 select max(app_id) over() m, row_number() over() s 
 from sys_app
 where year='2009'
) t
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