存储过程IDENTITY_INSERT

发布于 2024-11-16 17:32:25 字数 372 浏览 11 评论 0原文

我最近更改了我的数据表,删除了列并添加了一个新列,该列定义为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 pictureenter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

匿名。 2024-11-23 17:32:26

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 the IDENTITY_INSERT statements.

残龙傲雪 2024-11-23 17:32:26

只能在列时指定
使用列表并且 IDENTITY_INSERT 是
开。

您忘记了第一个条件:列列表。列列表(通常)是表名称和值之间的可选元素。您应该将其指定为:

INSERT INTO AirConditioner (ID, CategoricNumber, ...)
VALUES(@ID, @CategoricNumber, ...)

can only be specified when a column
list is used and IDENTITY_INSERT is
ON.

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:

INSERT INTO AirConditioner (ID, CategoricNumber, ...)
VALUES(@ID, @CategoricNumber, ...)
绿光 2024-11-23 17:32:26

不要使用设置标识插入。如果您有身份,则应该让 SQL Server 决定在其中放入什么值。

ALTER PROCEDURE [dbo].[AddAirConditioner] @CategoricNumber int, @Name nvarchar(50),
@PicName nvarchar(100), @ShortDetails nvarchar(200), @Details nvarchar(2000), 
@Price int, @ImagePath nvarchar(500), @AirConditionerType nvarchar(50), @COP float, 
@BTU float, @ProdType nvarchar(20), @ProdIndex int 
AS   

INSERT INTO AirConditioner VALUES( @CategoricNumber, @Name, @PicName, 
@ShortDetails, @Details, @Price, @ImagePath, @AirConditionerType, @COP,
@BTU, @ProdType, @ProdIndex) 

如果您需要取回 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.

ALTER PROCEDURE [dbo].[AddAirConditioner] @CategoricNumber int, @Name nvarchar(50),
@PicName nvarchar(100), @ShortDetails nvarchar(200), @Details nvarchar(2000), 
@Price int, @ImagePath nvarchar(500), @AirConditionerType nvarchar(50), @COP float, 
@BTU float, @ProdType nvarchar(20), @ProdIndex int 
AS   

INSERT INTO AirConditioner VALUES( @CategoricNumber, @Name, @PicName, 
@ShortDetails, @Details, @Price, @ImagePath, @AirConditionerType, @COP,
@BTU, @ProdType, @ProdIndex) 

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.

半山落雨半山空 2024-11-23 17:32:26

尝试放置

SET IDENTITY_INSERT [dbo].[AirConditioner] ON
GO

在更改程序之前

Try placing

SET IDENTITY_INSERT [dbo].[AirConditioner] ON
GO

Before the alter procedure

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文