Insert into … values ( SELECT … FROM … )

前端 未结 26 2487
我在风中等你
我在风中等你 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 05:55

    Postgres supports next: create table company.monitor2 as select * from company.monitor;

    0 讨论(0)
  • 2020-11-21 05:56

    To add something in the first answer, when we want only few records from another table (in this example only one):

    INSERT INTO TABLE1
    (COLUMN1, COLUMN2, COLUMN3, COLUMN4) 
    VALUES (value1, value2, 
    (SELECT COLUMN_TABLE2 
    FROM TABLE2
    WHERE COLUMN_TABLE2 like "blabla"),
    value4);
    
    0 讨论(0)
  • 2020-11-21 05:56

    For Microsoft SQL Server, I will recommend learning to interpret the SYNTAX provided on MSDN. With Google it's easier than ever, to look for syntax.

    For this particular case, try

    Google: insert site:microsoft.com

    The first result will be http://msdn.microsoft.com/en-us/library/ms174335.aspx

    scroll down to the example ("Using the SELECT and EXECUTE options to insert data from other tables") if you find it difficult to interpret the syntax given at the top of the page.

    [ WITH <common_table_expression> [ ,...n ] ]
    INSERT 
    {
            [ TOP ( expression ) [ PERCENT ] ] 
            [ INTO ] 
            { <object> | rowset_function_limited 
              [ WITH ( <Table_Hint_Limited> [ ...n ] ) ]
            }
        {
            [ ( column_list ) ] 
            [ <OUTPUT Clause> ]
            { VALUES ( { DEFAULT | NULL | expression } [ ,...n ] ) [ ,...n     ] 
            | derived_table       <<<<------- Look here ------------------------
            | execute_statement   <<<<------- Look here ------------------------
            | <dml_table_source>  <<<<------- Look here ------------------------
            | DEFAULT VALUES 
            }
        }
    }
    [;]
    

    This should be applicable for any other RDBMS available there. There is no point in remembering all the syntax for all products IMO.

    0 讨论(0)
  • 2020-11-21 05:57

    This is another example using values with select:

    INSERT INTO table1(desc, id, email) 
    SELECT "Hello World", 3, email FROM table2 WHERE ...
    
    0 讨论(0)
  • 2020-11-21 05:57

    Just use parenthesis for SELECT clause into INSERT. For example like this :

    INSERT INTO Table1 (col1, col2, your_desired_value_from_select_clause, col3)
    VALUES (
       'col1_value', 
       'col2_value',
       (SELECT col_Table2 FROM Table2 WHERE IdTable2 = 'your_satisfied_value_for_col_Table2_selected'),
       'col3_value'
    );
    
    0 讨论(0)
  • 2020-11-21 05:57

    I actually prefer the following in SQL Server 2008:

    SELECT Table1.Column1, Table1.Column2, Table2.Column1, Table2.Column2, 'Some String' AS SomeString, 8 AS SomeInt
    INTO Table3
    FROM Table1 INNER JOIN Table2 ON Table1.Column1 = Table2.Column3
    

    It eliminates the step of adding the Insert () set, and you just select which values go in the table.

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