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:

  1. get maximum old id table1 , write variable old_max_id.

  2. after generating result set, write row-numbers table1, maybe like

    insert table1 select row_number() on (partition <primary-key> order <whatever-you-want>)         + old_max_id      , current timestamp (<here comes sql query>) 
  3. either write result set table or return cursor it. here should either use same row_number statement above or directly use id table1.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -