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

前端 未结 15 2139
广开言路
广开言路 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: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 = '
            comment 1
            
        
        comment 2
            
        
        comment 3
            
        '
    
    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)
    

提交回复
热议问题