如何在 SQL 服务器级别启用 Identity_insert

How to enable Identity_insert on SQL Server Level

文章中https://support.microsoft.com/en-us/help/4568653/fix-insert-exec-doesn-t-work-when-you-insert-row-containing-explicit-i 它说:

INSERT EXEC doesn't work when you insert row containing explicit identity value into table with IDENTITY column and IDENTITY_INSERT is OFF by default in SQL Server

这意味着 IDENTITY_INSERT 可以在 SQL 服务器中默认设置为关闭,并以相同的方式打开。 有人知道如何实现吗?

您必须为要插入的任何 table 启用它,如下所示:

SET IDENTITY_INSERT [dbo].[tablename] ON

完成身份插入后,您必须将其禁用

SET IDENTITY_INSERT [dbo].[tablename] OFF

禁用它后你可以为另一个启用它table

documentation

请看这里

你不知道。 documentation 证实了这一点:

SET IDENTITY_INSERT (Transact-SQL)

Allows explicit values to be inserted into the identity column of a table.

...

Remarks

At any time, only one table in a session can have the IDENTITY_INSERT property set to ON. If a table already has this property set to ON, and a SET IDENTITY_INSERT ON statement is issued for another table, SQL Server returns an error message that states SET IDENTITY_INSERT is already ON and reports the table it is set ON for.

If the value inserted is larger than the current identity value for the table, SQL Server automatically uses the new inserted value as the current identity value.

The setting of SET IDENTITY_INSERT is set at execute or run time and not at parse time.

强调我的

如果您需要在多个 table 上启用它,您必须在一个 table 上启用它,禁用它,然后在下一个上启用它,依此类推