我有一个SQL Server数据库,想要更改自增列的起始值,因为它从 10010
开始,并且与另一个表相关联。现在我有200条记录,希望在记录数量增加之前解决这个问题。
最好的方式是如何更改或重置此列?
我有一个SQL Server数据库,想要更改自增列的起始值,因为它从 10010
开始,并且与另一个表相关联。现在我有200条记录,希望在记录数量增加之前解决这个问题。
最好的方式是如何更改或重置此列?
如果您需要将主键值更改为不同的数字(例如123-> 1123)。身份属性会阻止更改PK值。设置Identity_insert无法正常工作。如果您有级联删除,则不建议进行插入/删除操作(除非关闭引用完整性检查)。
编辑:较新版本的SQL不允许更改syscolumns实体,因此我的解决方案的一部分必须通过艰难的方式完成。请参考以下SO以从主键中删除Identity: Remove Identity from a column in a table 此脚本将关闭主键上的Identity:
***********************
sp_configure 'allow update', 1
go
reconfigure with override
go
update syscolumns set colstat = 0 --turn off bit 1 which indicates identity column
where id = object_id('table_name') and name = 'column_name'
go
exec sp_configure 'allow update', 0
go
reconfigure with override
go
***********************
select
'update ['+c.table_name+'] SET ['+Column_Name+']=['+Column_Name+']+1000000'
from Information_Schema.Columns as c
JOIN Information_Schema.Tables as t ON t.table_Name=c.table_name and t.Table_Schema=c.table_schema and t.table_type='BASE TABLE'
where Column_Name like 'CaseID' order by Ordinal_position
不允许对系统目录进行即席更新。
您可以使用sp_helptext 'sys.syscolumns'
查看视图文本。 - David Browne - Microsoft