我知道我几年前就这样做过,但我不记得语法了,而且由于调出大量关于“批量导入”的帮助文档和文章,我在任何地方都找不到它。
这是我想做的,但语法不完全正确…拜托,以前做过这件事的人帮帮我吧:)
INSERT INTO dbo.MyTable (ID, Name)
VALUES (123, 'Timmy'),
(124, 'Jonny'),
(125, 'Sally')
我知道这和正确的语法很接近。我可能需要"散装"这个词,我不记得了。任何想法?
我需要这个SQL Server 2005数据库。我试过这段代码,但没有效果:
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
我在关键字“值”附近得到不正确的语法。
这对于SQL Server 2008来说是OK的。对于SS2005及更早的版本,您需要重复VALUES语句。
INSERT INTO dbo.MyTable (ID, Name)
VALUES (123, 'Timmy')
VALUES (124, 'Jonny')
VALUES (125, 'Sally')
编辑:我的错。在SS2005中,您必须对每一行重复INSERT INTO。
INSERT INTO dbo.MyTable (ID, Name)
VALUES (123, 'Timmy')
INSERT INTO dbo.MyTable (ID, Name)
VALUES (124, 'Jonny')
INSERT INTO dbo.MyTable (ID, Name)
VALUES (125, 'Sally')
在PostgreSQL中,你可以这样做;
2列表的通用示例;
INSERT INTO <table_name_here>
(<column_1>, <column_2>)
VALUES
(<column_1_value>, <column_2_value>),
(<column_1_value>, <column_2_value>),
(<column_1_value>, <column_2_value>),
...
(<column_1_value>, <column_2_value>);
在这里查看真实世界的例子;
A -创建表
CREATE TABLE Worker
(
id serial primary key,
code varchar(256) null,
message text null
);
插入批量值
INSERT INTO Worker
(code, message)
VALUES
('a1', 'this is the first message'),
('a2', 'this is the second message'),
('a3', 'this is the third message'),
('a4', 'this is the fourth message'),
('a5', 'this is the fifth message'),
('a6', 'this is the sixth message');
插入多行
在多表插入中,将从子查询求值返回的行中导出的计算行插入到一个或多个表中。
无条件INSERT ALL:—如果要一次向表中添加多行,可以使用以下形式的INSERT语句:
INSERT ALL
INTO table_name (column_list) VALUES (value_list_1)
INTO table_name (column_list) VALUES (value_list_2)
INTO table_name (column_list) VALUES (value_list_3)
...
INTO table_name (column_list) VALUES (value_list_n)
SELECT 1 FROM DUAL; -- SubQuery
指定ALL后跟多个insert_into_clause子句来执行无条件的多表插入。Oracle数据库对子查询返回的每一行执行一次insert_into_clause子句。
MySQL服务器插入多行
INSERT INTO table_name (column_list)
VALUES
(value_list_1),
(value_list_2),
...
(value_list_n);
单行插入查询
INSERT INTO table_name (col1,col2) VALUES(val1,val2);
在SQL Server中使用XML插入多行会更容易,否则会变得非常乏味。
查看完整的文章与代码解释在这里http://www.cyberminds.co.uk/blog/articles/how-to-insert-multiple-rows-in-sql-server.aspx
将以下代码复制到sql server中以查看示例。
declare @test nvarchar(max)
set @test = '<topic><dialog id="1" answerId="41">
<comment>comment 1</comment>
</dialog>
<dialog id="2" answerId="42" >
<comment>comment 2</comment>
</dialog>
<dialog id="3" answerId="43" >
<comment>comment 3</comment>
</dialog>
</topic>'
declare @testxml xml
set @testxml = cast(@test as xml)
declare @answerTemp Table(dialogid int, answerid int, comment varchar(1000))
insert @answerTemp
SELECT ParamValues.ID.value('@id','int') ,
ParamValues.ID.value('@answerId','int') ,
ParamValues.ID.value('(comment)[1]','VARCHAR(1000)')
FROM @testxml.nodes('topic/dialog') as ParamValues(ID)
USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO
或者你可以用另一种方法
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES
('First',1),
('Second',2),
('Third',3),
('Fourth',4),
('Fifth',5)
这将达到你想要的效果:
INSERT INTO table1 (ID, Name)
VALUES (123, 'Timmy'),
(124, 'Jonny'),
(125, 'Sally');
对于未来的开发人员,您还可以从另一个表插入:
INSERT INTO table1 (ID, Name)
SELECT
ID,
Name
FROM table2
或者甚至从多个表:
INSERT INTO table1 (column2, column3)
SELECT
t2.column,
t3.column
FROM table2 t2
INNER JOIN table3 t3
ON t2.ID = t3.ID