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 ..."声明的一部分?
基础教程推荐
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 无法解决整理冲突 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
