存储过程IDENTITY_INSERT
我最近更改了我的数据表,删除了列并添加了一个新列,该列定义为identity = True,identity seeds = 1,identityincrement = 1。 当我尝试通过 STORE PROCEDURE 向此表插入数据时,出现此异常:
仅当使用列列表且 IDENTITY_INSERT 为 ON 时,才能指定表“AirConditioner”中标识列的显式值。
我发现我需要添加这一行: SET IDENTITY_INSERT [dbo].[AirConditioner] 打开,最后关闭 我添加了,它仍然抛出异常...
我的存储过程作为图片附加
I'm recently change my data table, I remove column and add a new column that define as identity = True and identity seed = 1, identity increment = 1.
When i tried to insert data to this table by STORE PROCEDURE i get this exception:
An explicit value for the identity column in table 'AirConditioner' can only be specified when a column list is used and IDENTITY_INSERT is ON.
I saw that i need to add this lines:
SET IDENTITY_INSERT [dbo].[AirConditioner] ON and finally OFF
I added and its still throw an exception...
My store procedure is attached as a picture
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SQL Server 将负责为您的标识列分配一个值。您应该从插入语句中删除
@ID
并删除IDENTITY_INSERT
语句。SQL Server will take care of assigning a value to your identity column. You should remove
@ID
from the insert statement and remove theIDENTITY_INSERT
statements.您忘记了第一个条件:列列表。列列表(通常)是表名称和值之间的可选元素。您应该将其指定为:
You're forgetting the first condition: the column list. The column list is a (usually) optional element between the table name and
values
. You should specify it like:不要使用设置标识插入。如果您有身份,则应该让 SQL Server 决定在其中放入什么值。
如果您需要取回 ID 以在子表中使用,请使用scope_identity 或output 子句。在在线书籍中查找这些内容以了解如何使用。
Do NOT use set identity insert on. If you have an identity, you should be letting SQL server decide what value to put in there.
If you need to get the ID back for using in child tables, then use scope_identity or the output clause. Look these up in Books online to see how to use.
尝试放置
在更改程序之前
Try placing
Before the alter procedure