SET IDENTITY_INSERT 给出错误 - IDENTITY_INSERT 已经为 table 打开

SET IDENTITY_INSERT gives an error - IDENTITY_INSERT is already ON for table

使用简单的插入语句,我尝试使用 PK 密钥在 table 中显式插入数据,这些密钥本身定义了身份 属性。但是当我 运行 下面的语句时,我收到了一条奇怪的错误消息

SET IDENTITY_INSERT MyDB.dbo.MyTable ON

Msg 8107, Level 16, State 1, Line 163
IDENTITY_INSERT is already ON for table 'MyDB.dbo.AnotherTable'. Cannot perform SET operation for table 'MyDB.dbo.MyTable '.

为什么在为 MyDB.dbo.MyTable 指定此 Identity_Insert 时在 MyDB.dbo.AnotherTable 上抱怨?

其他一些table也给出了类似的错误。

问题是我与服务器的 SQL Server 2016 连接。断开连接并重新连接后,一切正常!

根据 documentation:

At any time, only one table in a session can have the IDENTITY_INSERT property set to ON.

如果您需要在多个 table 中插入明确的 IDENTITY 值,请确保在移动到下一个 table 之前将其设置为 OFF。