本文介绍了如何使用 SQL Server 2008 打开和关闭 IDENTITY_INSERT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IDENTITY_INSERT 设置为 OFF 时,为什么我在执行插入时出错?

Why am I getting an error doing an insert when IDENTITY_INSERT is set to OFF?

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

How do I turn it on properly in SQL Server 2008? Is it by using SQL Server Management Studio?

我已经运行了这个查询:

I have run this query:

SET IDENTITY_INSERT Database. dbo. Baskets ON

然后我在控制台中收到消息,表明命令已成功完成.但是,当我运行该应用程序时,它仍然给我如下所示的错误:

Then I got the message back in the console that the Command(s) completed successfully.However when I run the application, it still gives me the error shown below:

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

推荐答案

Via SQL as per MSDN

Via SQL as per MSDN

SET IDENTITY_INSERT sometableWithIdentity ON

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

SET IDENTITY_INSERT sometableWithIdentity OFF

完整的错误消息会告诉您确切地出了什么问题...

The complete error message tells you exactly what is wrong...

当 IDENTITY_INSERT 设置为 OFF 时,无法为表 'sometableWithIdentity' 中的标识列插入显式值.

这篇关于如何使用 SQL Server 2008 打开和关闭 IDENTITY_INSERT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 16:59