Insert into … values ( SELECT … FROM … )

前端 未结 26 2420
我在风中等你
我在风中等你 2020-11-21 05:40

I am trying to INSERT INTO a table using the input from another table. Although this is entirely feasible for many database engines, I always seem to struggle t

相关标签:
26条回答
  • 2020-11-21 06:06

    Instead of VALUES part of INSERT query, just use SELECT query as below.

    INSERT INTO table1 ( column1 , 2, 3... )
    SELECT col1, 2, 3... FROM table2
    
    0 讨论(0)
  • 2020-11-21 06:06

    In informix it works as Claude said:

    INSERT INTO table (column1, column2) 
    VALUES (value1, value2);    
    
    0 讨论(0)
  • 2020-11-21 06:07

    Here is another example where source is taken using more than one table:

    INSERT INTO cesc_pf_stmt_ext_wrk( 
      PF_EMP_CODE    ,
      PF_DEPT_CODE   ,
      PF_SEC_CODE    ,
      PF_PROL_NO     ,
      PF_FM_SEQ      ,
      PF_SEQ_NO      ,
      PF_SEP_TAG     ,
      PF_SOURCE) 
    SELECT
      PFl_EMP_CODE    ,
      PFl_DEPT_CODE   ,
      PFl_SEC         ,
      PFl_PROL_NO     ,
      PF_FM_SEQ       ,
      PF_SEQ_NO       ,
      PFl_SEP_TAG     ,
      PF_SOURCE
     FROM cesc_pf_stmt_ext,
          cesc_pfl_emp_master
     WHERE pfl_sep_tag LIKE '0'
       AND pfl_emp_code=pf_emp_code(+);
    
    COMMIT;
    
    0 讨论(0)
  • 2020-11-21 06:07

    Here's how to insert from multiple tables. This particular example is where you have a mapping table in a many to many scenario:

    insert into StudentCourseMap (StudentId, CourseId) 
    SELECT  Student.Id, Course.Id FROM Student, Course 
    WHERE Student.Name = 'Paddy Murphy' AND Course.Name = 'Basket weaving for beginners'
    

    (I realise matching on the student name might return more than one value but you get the idea. Matching on something other than an Id is necessary when the Id is an Identity column and is unknown.)

    0 讨论(0)
  • 2020-11-21 06:16

    Most of the databases follow the basic syntax,

    INSERT INTO TABLE_NAME
    SELECT COL1, COL2 ...
    FROM TABLE_YOU_NEED_TO_TAKE_FROM
    ;
    

    Every database I have used follow this syntax namely, DB2, SQL Server, MY SQL, PostgresQL

    0 讨论(0)
  • 2020-11-21 06:17

    IF you want to insert some data into a table without want to write column name.

    INSERT INTO CUSTOMER_INFO
       (SELECT CUSTOMER_NAME,
               MOBILE_NO,
               ADDRESS
          FROM OWNER_INFO cm)
    

    Where the tables are:

                CUSTOMER_INFO               ||            OWNER_INFO
    ----------------------------------------||-------------------------------------
    CUSTOMER_NAME | MOBILE_NO | ADDRESS     || CUSTOMER_NAME | MOBILE_NO | ADDRESS 
    --------------|-----------|---------    || --------------|-----------|--------- 
          A       |     +1    |   DC        ||       B       |     +55   |   RR  
    

    Result:

                CUSTOMER_INFO               ||            OWNER_INFO
    ----------------------------------------||-------------------------------------
    CUSTOMER_NAME | MOBILE_NO | ADDRESS     || CUSTOMER_NAME | MOBILE_NO | ADDRESS 
    --------------|-----------|---------    || --------------|-----------|--------- 
          A       |     +1    |   DC        ||       B       |     +55   |   RR
          B       |     +55   |   RR        ||
    
    0 讨论(0)
提交回复
热议问题