sql - How to insert generated id into a results table -
i have following query
select q.pol_id quot q ,fgn_clm_hist fch q.quot_id = fch.quot_id union select q.pol_id tdb2wccu.quot q q.nr_prr_ls_yr_cov not null
for every row in result set, want create new row in table (call table1) , update pol_id in quot table (from above result set) generated primary key inserted row in table1.
table1 has 2 columns. id , timestamp.
i'm using db2 10.1.
i've tried numerous things , have been unsuccessful quite while. thanks!
simple solution: create new table result set of query, has identity column in it. then, after running query, update pol_id
field newly generated id in result table.
alteratively, can more manually using row_number()
olap function, found convenient creating ids. convenient use stored procedure following:
get maximum old id
table1
, write variableold_max_id
.after generating result set, write row-numbers
table1
, maybe likeinsert table1 select row_number() on (partition <primary-key> order <whatever-you-want>) + old_max_id , current timestamp (<here comes sql query>)
either write result set table or return cursor it. here should either use same
row_number
statement above or directly use idtable1
.
Comments
Post a Comment