简体   繁体   English

Sql Server 中的多次插入

[英]Multiple insert in Sql Server

I want to perform multiple insert in my sql server 2008 DB.我想在我的 sql server 2008 DB 中执行多次插入。

The insert looks like;插入看起来像;

--pseudocode:

INSERT INTO DOCS (c1,c2) 
VALUES ('v1','v2'); 

SELECT @iddoc= scope_identity(); 

UPDATE table2 
SET y1='y1',y2='y2' 
WHERE iddoc=@iddoc;

This statement 50000 times.此语句50000次。

I have a file batch (.sql) but I have problem with memory.我有一个文件批处理 (.sql),但内存有问题。

Try like this, using GO statement像这样尝试,使用 GO 语句

INSERT INTO DOCS (c1,c2) 
VALUES ('v1','v2'); 

SELECT @iddoc= scope_identity(); 

UPDATE table2 
SET y1='y1',y2='y2' 
WHERE iddoc=@iddoc;

GO 50000

I suspect you are trying to load a table (DOCS) with rows from a CSV file.我怀疑您正在尝试使用 CSV 文件中的行加载表 (DOCS)。 For that, the easiest way would be to write an SSIS package or use the import/export wizard to load the data quickly.为此,最简单的方法是编写 SSIS 包或使用导入/导出向导快速加载数据。

What I'm not clear about is what "table2" is used for.我不清楚的是“table2”是做什么用的。 I'm guessing it is taking the same row data from the CSV but splits different columns into a seperate table.我猜它是从 CSV 中获取相同的行数据,但将不同的列拆分为一个单独的表。 Easiest way to do this, if the data volume is low (like it is now), load everything into one table and then execute a single insert.. select... statement to bulk copy part of the table columns into table 2.最简单的方法是,如果数据量很低(就像现在这样),将所有内容加载到一个表中,然后执行单个 insert.. select... 语句将部分表列批量复制到表 2 中。

@treep - you can accomplish this task with a while loop. @treep - 您可以使用 while 循环完成此任务。

DECLARE @a INT = 1

DECLARE @tmpId TABLE
(
    newid INT
)

WHILE(@a < 50000)
BEGIN
    INSERT INTO DOCS (c1,c2)
    VALUES ('v1','v2'); 

    INSERT INTO @tmpId
    SELECT scope_identity(); 
    SET @a = @a + 1  
END

UPDATE T2
SET T2.y1='y1',T2.y2='y2' 
FROM table2 T2
INNER JOIN @tmpId TM ON TM.ID = T2.iddoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM