Insert db2 query from another table

I have a table product (id_product, name);

I have one more: productHistory (id_H, id_product, name);

I want to create a query (db2) to insert all product lines in productHistory;

I have a product_history_seq sequence

I want to do something like this:

insert into productHistory (id_h , , id_product , name) values ( product_history_seq.nextval,.. 

Or

 select (id_product , name) from product 

What is the correct request?

+4
source share
4 answers

I believe what you are looking for:

 insert into productHistory ( id_h , id_product , name ) select next value for product_history_seq , id_product , name from product ; 
+2
source

Make id_h increase and try

  insert into productHistory ( id_product , name) values (select id_product , name from product ); 

id_h will automatically increase, no need to put it in the request

Hope this helps

+2
source
 INSERT INTO productHistory (id_h, id_product, name) (SELECT product_history_seq.nextval, id_product, name FROM product); 

It works

+1
source

"insert into your column select default, val1, val2 from tabletableww" and declare id as genereated by default

0
source

Source: https://habr.com/ru/post/1494899/


All Articles