且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQL Server中以相同的ID在SQL Server中运行插入数据脚本

更新时间:2021-11-11 01:05:16

在插入之前,请执行以下操作每个表的声明.

例如:

Before inserting execute the below statement for each table.

for example:

SET IDENTITY_INSERT table_name ON
insert into ...
SET IDENTITY_INSERT table_name Off



如果db中的每个表都有一个标识字段,则可以一次插入所有表的标识



if every table in db has an identity field then you can on identity insert for all tables at a time

EXEC sp_MSforeachtable @command1="SET IDENTITY_INSERT ? OFF"


取决于条件
如果要添加具有相同ID的数据,
然后不要将您的ID列设为主键,也不要应用唯一键.

如果您希望该ID应该是唯一的,但必须将数据插入具有新ID的表中.
然后使ID列自动递增
并从脚本中删除插入查询脚本中的列,值列表中的ID列和ID列的值.

祝您编程愉快!
:)
depends on criteria
if you want to add data with same id,
then do not make your Id column primary key and do not apply unique key.

if you want that id should be unique but data must be insert in table with new id.
then make Id column auto increment
and from script remove id column & value for id column from column,value list in script of insert query.

Happy coding!
:)


感谢所有我自己解决的问题....


感谢您的回复...
Thanks to all I solved it myself....


Thanks for your reply...