db2 query insert from another table

后端 未结 4 499
我在风中等你
我在风中等你 2021-01-12 15:22

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 row

相关标签:
4条回答
  • 2021-01-12 15:58

    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 
    ;
    
    0 讨论(0)
  • 2021-01-12 16:00
    INSERT INTO productHistory (id_h, id_product, name)
      (SELECT
        product_history_seq.nextval,
        id_product,
        name
      FROM product);
    

    That works

    0 讨论(0)
  • 2021-01-12 16:01

    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

    0 讨论(0)
  • 2021-01-12 16:18

    "insert into yourtableone select default, val1, val2 from yourtabletwo" and declare the id as genereated by default

    0 讨论(0)
提交回复
热议问题