Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

sql - 如何使用SQL Server 2008打开和关闭IDENTITY_INSERT?(How to turn IDENTITY_INSERT on and off using SQL Server 2008?)

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

(当IDENTITY_INSERT设置为OFF时,为什么我在插入时出错?)

How do I turn it on properly in SQL Server 2008?

(如何在SQL Server 2008中正确启用它?)

Is it by using SQL Server Management Studio?

(是使用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.
  ask by Beginner translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Via SQL as per MSDN

(根据MSDN通过SQL)

SET IDENTITY_INSERT sometableWithIdentity ON

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

SET IDENTITY_INSERT sometableWithIdentity OFF

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

(完整的错误消息告诉您到底出了什么问题......)

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

(当IDENTITY_INSERT设置为OFF时,无法在表'sometableWithIdentity'中为identity列插入显式值。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...