简体   繁体   English

如何在SQL Server中为多个插入创建批处理ID

[英]How do i make a batch ID for multiple inserts in sql server

I am trying to insert multiple records on my database using a simple insert query and looping it using foreach statement. 我试图使用一个简单的插入查询在数据库中插入多个记录,并使用foreach语句将其循环。 Now I want to make a Batch ID for all the rows inserted and it has to be the same for all rows inserted. 现在,我要为所有插入的行创建一个批处理ID,并且对于所有插入的行,它必须相同。 The format of the batch ID I made is like this 1004160001. How can I achieve this? 我制作的批次ID的格式类似于1004160001。如何实现?

I just need suggestion guys. 我只需要建议人。 Thank you. 谢谢。

Can't you just insert a string value '1004160001' (or an int of course)? 您不能只插入字符串值'1004160001'(当然是int)吗?

INSERT INTO (batch_id, data) VALUES('1004160001', 'row data'); INSERT INTO(batch_id,data)VALUES('1004160001','row data');

SET @BatchNo = '1004160001' Loop Start SET @BatchNo ='1004160001'循环开始

INSERT INTO MyTable(BatchNo,Slno,Name,Amount) SELECT @BatchNo,i,@Name,@Amount 插入MyTable(BatchNo,Slno,Name,Amount)SELECT @ BatchNo,i,@ Name,@ Amount

Loop End 循环结束

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

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