问题
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