SQL Server 2005: Insert multiple rows with single query

后端 未结 5 1042
北海茫月
北海茫月 2020-12-15 20:56

This should be a fairly straightforward question, but I haven\'t been able to find a solid answer online. I\'m trying to insert multiple rows into the same table, but with o

5条回答
  •  不知归路
    2020-12-15 21:49

    Since MS SQLServer 2005 supports XML the best method I would suggest is a STORED PROCEDURE with an input parameter of XML type. If you are working with .NET you can easily convert the DataSet to xml string using ds.GetXml() method and can be sent to the SP

    CREATE PROCEDURE [dbo].[insertLocation](@XML XML=NULL)
    AS
    BEGIN
      INSERT INTO [dbo].[TheLocations]
            ( [Name], [Location] )
       SELECT
            XTab.value('Name[1]','nvarchar(100)') AS[Name],
            XTab.value('Location[1]','nvarchar(200)') AS[Location]
        FROM @XML.nodes('TheLocations') XTab([XTab])
    END
    

提交回复
热议问题