Insert multiple rows WITHOUT repeating the “INSERT INTO …” part of the statement?

前端 未结 15 1884
广开言路
广开言路 2020-11-22 09:16

I know I\'ve done this before years ago, but I can\'t remember the syntax, and I can\'t find it anywhere due to pulling up tons of help docs and articles about \"bulk import

相关标签:
15条回答
  • 2020-11-22 09:41

    Oracle SQL Server Insert Multiple Rows

    In a multitable insert, you insert computed rows derived from the rows returned from the evaluation of a subquery into one or more tables.

    Unconditional INSERT ALL:- To add multiple rows to a table at once, you use the following form of the INSERT statement:

    INSERT ALL
       INTO table_name (column_list) VALUES (value_list_1)
       INTO table_name (column_list) VALUES (value_list_2)
       INTO table_name (column_list) VALUES (value_list_3)
       ...
       INTO table_name (column_list) VALUES (value_list_n)
    SELECT 1 FROM DUAL; -- SubQuery
    

    Specify ALL followed by multiple insert_into_clauses to perform an unconditional multitable insert. Oracle Database executes each insert_into_clause once for each row returned by the subquery.

    MySQL Server Insert Multiple Rows

    INSERT INTO table_name (column_list)
    VALUES
        (value_list_1),
        (value_list_2),
        ...
        (value_list_n);
    

    Single Row insert Query

    INSERT INTO table_name (col1,col2) VALUES(val1,val2);
    
    0 讨论(0)
  • 2020-11-22 09:42

    This will achieve what you're asking about:

    INSERT INTO table1 (ID, Name)
        VALUES (123, 'Timmy'), 
               (124, 'Jonny'), 
               (125, 'Sally');
    

    For future developers, you can also insert from another table:

    INSERT INTO table1 (ID, Name)
        SELECT 
             ID, 
             Name 
        FROM table2
    

    Or even from multiple tables:

    INSERT INTO table1 (column2, column3)
        SELECT 
             t2.column, 
             t3.column
        FROM table2 t2
             INNER JOIN table3 t3
             ON t2.ID = t3.ID
    
    0 讨论(0)
  • 2020-11-22 09:44

    If your data is already in your database you can do:

    INSERT INTO MyTable(ID, Name)
    SELECT ID, NAME FROM OtherTable
    

    If you need to hard code the data then SQL 2008 and later versions let you do the following...

    INSERT INTO MyTable (Name, ID)
    VALUES ('First',1),
    ('Second',2),
    ('Third',3),
    ('Fourth',4),
    ('Fifth',5)
    
    0 讨论(0)
  • 2020-11-22 09:47

    It would be easier to use XML in SQL Server to insert multiple rows otherwise it becomes very tedious.

    View full article with code explanations here http://www.cyberminds.co.uk/blog/articles/how-to-insert-multiple-rows-in-sql-server.aspx

    Copy the following code into sql server to view a sample.

    declare @test nvarchar(max)
    
    set @test = '<topic><dialog id="1" answerId="41">
            <comment>comment 1</comment>
            </dialog>
        <dialog id="2" answerId="42" >
        <comment>comment 2</comment>
            </dialog>
        <dialog id="3" answerId="43" >
        <comment>comment 3</comment>
            </dialog>
        </topic>'
    
    declare @testxml xml
    set @testxml = cast(@test as xml)
    declare @answerTemp Table(dialogid int, answerid int, comment varchar(1000))
    
    insert @answerTemp
    SELECT  ParamValues.ID.value('@id','int') ,
    ParamValues.ID.value('@answerId','int') ,
    ParamValues.ID.value('(comment)[1]','VARCHAR(1000)')
    FROM @testxml.nodes('topic/dialog') as ParamValues(ID)
    
    0 讨论(0)
  • 2020-11-22 09:49

    You can use a union:

    INSERT INTO dbo.MyTable (ID, Name) 
    SELECT ID, Name FROM (
        SELECT 123, 'Timmy'
        UNION ALL
        SELECT 124, 'Jonny'
        UNION ALL
        SELECT 125, 'Sally'
    ) AS X (ID, Name)
    
    0 讨论(0)
  • 2020-11-22 09:49

    Others here have suggested a couple multi-record syntaxes. Expounding upon that, I suggest you insert into a temp table first, and insert your main table from there.

    The reason for this is loading the data from a query can take longer, and you may end up locking the table or pages longer than is necessary, which slows down other queries running against that table.

    -- Make a temp table with the needed columns
    select top 0 *
    into #temp
    from MyTable (nolock)
    
    -- load data into it at your leisure (nobody else is waiting for this table or these pages)
    insert #temp (ID, Name)
    values (123, 'Timmy'),
    (124, 'Jonny'),
    (125, 'Sally')
    
    -- Now that all the data is in SQL, copy it over to the real table. This runs much faster in most cases.
    insert MyTable (ID, Name)
    select ID, Name
    from #temp
    
    -- cleanup
    drop table #temp
    

    Also, your IDs should probably be identity(1,1) and you probably shouldn't be inserting them, in the vast majority of circumstances. Let SQL decide that stuff for you.

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