Insert multiple rows WITHOUT repeating the quot;INSERT INTO ...quot; part of the statement?(插入多行而不重复“INSERT INTO ...声明的一部分?)
问题描述
我知道几年前我已经这样做了,但是我不记得语法了,而且由于查阅了大量关于批量导入"的帮助文档和文章,我无法在任何地方找到它.
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 imports".
这是我想要做的,但语法不完全正确......请以前做过这个的人帮帮我:)
Here's what I want to do, but the syntax is not exactly right... please, someone who has done this before, help me out :)
INSERT INTO dbo.MyTable (ID, Name)
VALUES (123, 'Timmy'),
(124, 'Jonny'),
(125, 'Sally')
我知道这是接近正确的语法.我可能需要在其中使用BULK"这个词,或者其他什么,我不记得了.有什么想法吗?
I know that this is close to the right syntax. I might need the word "BULK" in there, or something, I can't remember. Any idea?
我需要这个用于 SQL Server 2005 数据库.我已经尝试过这段代码,但无济于事:
I need this for a SQL Server 2005 database. I've tried this code, to no avail:
DECLARE @blah TABLE
(
ID INT NOT NULL PRIMARY KEY,
Name VARCHAR(100) NOT NULL
)
INSERT INTO @blah (ID, Name)
VALUES (123, 'Timmy')
VALUES (124, 'Jonny')
VALUES (125, 'Sally')
SELECT * FROM @blah
我收到关键字VALUES"附近的语法不正确.
推荐答案
INSERT INTO dbo.MyTable (ID, Name)
SELECT 123, 'Timmy'
UNION ALL
SELECT 124, 'Jonny'
UNION ALL
SELECT 125, 'Sally'
对于 SQL Server 2008,可以根据问题中的语句在一个 VALUES 子句中完全执行此操作(您只需添加一个逗号来分隔每个值语句)...
For SQL Server 2008, can do it in one VALUES clause exactly as per the statement in your question (you just need to add a comma to separate each values statement)...
这篇关于插入多行而不重复“INSERT INTO ..."声明的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:插入多行而不重复“INSERT INTO ..."声明的一部分?


基础教程推荐
- 带更新的 sqlite CTE 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01