这个问题在这里已有答案:
- How do I insert multiple rows WITHOUT repeating the “INSERT INTO dbo.Blah” part of the statement? 13个答案
我有一组数据要一次插入,比如4行 .
我的表有三列: Person
, Id
和 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");
我可以 insert
单个 SQL statement
中的所有4行吗?
4 回答
INSERT
使用VALUES
语法的语句可以插入多行 . 为此,请包含多个列值列表,每个列值都括在括号中并用逗号分隔 .Example:
NOTE: 这个答案适用于SQL Server 2005.对于SQL Server 2008及更高版本,有更好的方法,如其他答案所示 .
你可以使用INSERT with SELECT UNION ALL:
仅适用于小型数据集,对于您的4条记录应该没问题 .
如果要插入单个表,则可以像这样编写查询(可能只在MySQL中):
在SQL Server 2008中,您可以使用单个SQL INSERT语句插入多行 .
有关参考,请参阅MOC课程2778A - 在SQL Server 2008中编写SQL查询 .
例如: