如何在SQL Server中以相同的ID在SQL Server中运行插入数据脚本 [英] How to run a Insert data Script in SQL Server with same id available in SQL DB

查看:84
本文介绍了如何在SQL Server中以相同的ID在SQL Server中运行插入数据脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a Insert Data Script with IDs.... and in SQL database have some records in same table....

when i run Insert data script there is problem in inserting Insert Data script record in same table because SQL db table and my Insert data script have same IDs


So how to resolve that problem....

推荐答案

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

例如:

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...


这篇关于如何在SQL Server中以相同的ID在SQL Server中运行插入数据脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆