在单个 SQL 查询中插入多行?

2021-12-01 00:00:00 sql insert tsql sql-server

我要一次插入多组数据,比如说 4 行.我的表有三列:PersonIdOffice.

I have multiple set of data to insert at once, say 4 rows. My table has three columns: Person, Id and Office.

INSERT INTO MyTable VALUES ("John", 123, "Lloyds Office");
INSERT INTO MyTable VALUES ("Jane", 124, "Lloyds Office");
INSERT INTO MyTable VALUES ("Billy", 125, "London Office");
INSERT INTO MyTable VALUES ("Miranda", 126, "Bristol Office");

我可以在一个 SQL 语句中插入所有 4 行吗?

Can I insert all 4 rows in a single SQL statement?

推荐答案

在 SQL Server 2008 中,您可以使用单个 SQL INSERT 语句插入多行.

In SQL Server 2008 you can insert multiple rows using a single SQL INSERT statement.

INSERT INTO MyTable ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )

有关此内容的参考,请查看 MOC 课程 2778A - 在 SQL Server 2008 中编写 SQL 查询.

For reference to this have a look at MOC Course 2778A - Writing SQL Queries in SQL Server 2008.

例如:

INSERT INTO MyTable
  ( Column1, Column2, Column3 )
VALUES
  ('John', 123, 'Lloyds Office'), 
  ('Jane', 124, 'Lloyds Office'), 
  ('Billy', 125, 'London Office'),
  ('Miranda', 126, 'Bristol Office');

相关文章