SQL Server 2005: Insert multiple rows with single query

半腔热情 提交于 2019-12-18 03:11:13

问题


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 only one statement. The most popular I've seen online is the following, but I've read that it only works with SQL Server 2008:

INSERT INTO Table (Name, Location) VALUES
('Name1', 'Location1'),
('Name2', 'Location2'),
('Name3', 'Location3'), etc...

I'd prefer this method if it will work with SQL Server 2005, but I don't think it will. The other option, from what I've read, has to do with UNION ALL's following SELECT statements after the INSERT, which seems clunky. Does anyone know for sure the best syntax to do this in 2005?

Thanks.


回答1:


Yep. You have to use UNION ALLs in SQL Server 2005 to insert multiple rows in a SQL script in a single statement.

INSERT INTO Table 
  (Name, Location) 
SELECT 'Name1', 'Location1' 
UNION ALL
SELECT 'Name2', 'Location2'
UNION ALL
SELECT 'Name3', 'Location3' 

The other main alternative is to repeat the Insert statement multiple times which is even more verbose. You need to be careful to use Explicit transactions in this last case to avoid the overhead of many individual commits (and for atomicity reasons of course)

If you have lots of rows to insert you could use BULK INSERT to load it all in from a delimited file in one statement.

Finally if this is data already in the database that you are scripting out (perhaps to deploy on another server) the SSMS Tools Pack addin has a "Generate Insert Statements" function that can generate these statements for you.




回答2:


As others have said, the key here is UNION ALL. For me, using a CTE keeps things looking a little cleaner e.g.

WITH NewStuff (Name, Location)
     AS
     (
      SELECT 'Name1', 'Location1' UNION ALL
      SELECT 'Name2', 'Location2' UNION ALL
      SELECT 'Name3', 'Location3' 
     )
INSERT INTO Stuff (Name, Location) 
SELECT Name, Location
  FROM NewStuff; 



回答3:


You have to use the union all in sql server 2005. To be honest, that's so clunky and ugly, I'd just use multiple inserts if I were you. Wrap them in a single transaction and it's the same thing in the end.




回答4:


Yes they are your only options, unless you are inserting a lot of data and might want to explore a BULK INSERT

INSERT INTO Table (Name, Location)
SELECT 'Name1', 'Location1' UNION ALL
SELECT 'Name2', 'Location2' UNION ALL
SELECT 'Name3', 'Location3' 



回答5:


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


来源:https://stackoverflow.com/questions/3272487/sql-server-2005-insert-multiple-rows-with-single-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!