Ad hoc updates to system catalogs are not allowed in SQL Server 2012

时光怂恿深爱的人放手 提交于 2021-02-19 06:56:06

问题


I want to remove identity from a column by updating it like this..

sp_configure 'allow update', 1 
go 

reconfigure with override 
go

update sys.syscolumns 
set colstat = 0    -- turn off bit 1 which indicates identity column 
where id = object_id('tbl1') and name = 'ids'
go

sp_configure 'allow updates', 0 
go 

reconfigure 
go

I am getting this error, tried many times.

Msg 259, Level 16, State 1, Line 15
Ad hoc updates to system catalogs are not allowed.


回答1:


If you want to get rid of it completely, just rename the table and then dump the data into a new table.

EXEC sp_rename 'OriginalTblName','OLD_OriginalTblName'

CREATE TABLE OriginalTblName (Definition of your Table)

INSERT OriginalTblName
SELECT * FROM OLD_OriginalTblName

You can skip the CREATE TABLE step if you want by just selecting the contents into the new table. You lose the ability to define the fields the way you want with this method.

SELECT * FROM OLD_OriginalTblName
INTO OriginalTblName

If you are just wanting to INSERT new records, you can use IDENTITY INSERT to insert the records you want. Just be careful not to duplicate the values or you will break the table.

SET IDENTITY_INSERT ON OriginalTblName

INSERT OriginalTblName
SELECT someFields
FROM someTbl

SET IDENTITY_INSERT OFF OriginalTblName

IDENTITY INSERT will not work for UPDATE on the IDENTITY field. You will need to capture the data and reinsert the record with one of the methods described above.



来源:https://stackoverflow.com/questions/34336115/ad-hoc-updates-to-system-catalogs-are-not-allowed-in-sql-server-2012

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!