简体   繁体   中英

db2 query insert from another table

I have a table product(id_product , name );

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

I wanna create a query (db2) to insert all the rows of product in productHistory;

I have a sequence product_history_seq

I wanna do something like that:

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

Or,

select (id_product , name) from product

What's the correct query?

I believe you are looking for:

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

Make id_h auto increment and try this

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

id_h will auto-increment no need to put it in query

Hope it will help

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

That works

“插入到您的tableone中,选择默认值,从您的tabletwo中选择val1,val2”,并将该id声明为默认生成的

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM