小编典典

如何使用SQL Server 2008打开和关闭IDENTITY_INSERT?

sql

IDENTITY_INSERT设置为OFF时为什么插入时出错?

如何在SQL Server 2008中正确打开它?是通过使用SQL Server Management Studio吗?

我已经运行了这个查询:

SET IDENTITY_INSERT Database. dbo. Baskets ON

然后,我在控制台中收到一条消息,指出命令已成功完成。但是,当我运行该应用程序时,它仍然给我显示以下错误:

Cannot insert explicit value for identity column in table 'Baskets' when 
IDENTITY_INSERT is set to OFF.

阅读 224

收藏
2021-05-23

共1个答案

小编典典

通过SQL按照MSDN

SET IDENTITY_INSERT sometableWithIdentity ON

INSERT INTO sometableWithIdentity 
    (IdentityColumn, col2, col3, ...)
VALUES 
    (AnIdentityValue, col2value, col3value, ...)

SET IDENTITY_INSERT sometableWithIdentity OFF

完整的错误消息会告诉您确切的问题是什么…

IDENTITY_INSERT设置为OFF时,无法为表'sometableWithIdentity'中的Identity列插入显式值。

2021-05-23