Set IDENTITY_INSERT OFF for all tables

前端 未结 5 1934
轮回少年
轮回少年 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:37

    Dynamic SQL:

    select 'set identity_insert ['+s.name+'].['+o.name+'] off'
    from sys.objects o
    inner join sys.schemas s on s.schema_id=o.schema_id
    where o.[type]='U'
    and exists(select 1 from sys.columns where object_id=o.object_id and is_identity=1)
    

    Then copy & paste the resulting SQL into another query window and run

提交回复
热议问题