Set IDENTITY_INSERT OFF for all tables

前端 未结 5 1932
轮回少年
轮回少年 2020-12-28 14:02

I have a script which creates an entire database and inserts all records to a few dozen tables. It works great, unless there is some issue during the processing, and a table

5条回答
  •  攒了一身酷
    2020-12-28 14:47

    EXEC sp_MSforeachtable @command1="PRINT '?'; SET IDENTITY_INSERT ? OFF",
    @whereand = ' AND EXISTS (SELECT 1 FROM sys.columns WHERE object_id = o.id  AND is_identity = 1)'
    

    Building on Lynn's answer, in case you're too lazy to perform this in more than one step - this should run on all tables where there is an identity column.

    Caveat is only tested in 2012 and sp_MSforeachtable is of course entirely unsupported...

提交回复
热议问题