无法为标识列插入显式值 [英] Cannot insert explicit value for identity column
问题描述
当IDENTITY_INSERT设置为OFF时,无法在表'Producto'中为identity列插入显式值。
alter proc uspIdatos_
@iDProducto int output,
@Descripcion nvarchar(50),
@Precio money,
@Stock int,
@Notas text
as
插入到Producto(IDProducto,Descripcion,Precio,Stock,Notas)值(@ iDProducto,@ Descripcion,@ Precio,@ Stock,@ Notas)
SET @iDProducto = @@ IDENTITY
Cannot insert explicit value for identity column in table 'Producto' when IDENTITY_INSERT is set to OFF.
alter proc uspIdatos_
@iDProducto int output,
@Descripcion nvarchar(50),
@Precio money,
@Stock int,
@Notas text
as
Insert into Producto(IDProducto,Descripcion,Precio,Stock,Notas)values(@iDProducto,@Descripcion,@Precio,@Stock,@Notas)
SET @iDProducto=@@IDENTITY
推荐答案
不,你不能。
但是...如果你将@iDProducto声明为INPUT值而不是OUTPUT,也许你可以......但是你无法返回它。
所以make你的想法:这是一个身份字段,SQL应该管理它,你应该返回它;或者它不是,它是从外部指定的,因此不需要返回它。
No, you can't.
But...if you declared @iDProducto as an INPUT value rather than an OUTPUT, perhaps you could...but then you couldn't return it.
So make your mind up: either this is an identity field, SQL should manage it, and you should return it; or it isn't, it is specified from outside, and so there is no need to return it.
您可以在插入命令之前使用此命令。
You can use this command before your insert command.
SET IDENTITY_INSERT dbo.Producto ON;
alter proc uspIdatos_
@iDProducto int output,
@Descripcion nvarchar(50),
@Precio money,
@Stock int,
@Notas text
as
SET IDENTITY_INSERT Producto ON;
Insert into Producto(IDProducto,Descripcion,Precio,Stock,Notas)values(@iDProducto,@Descripcion,@Precio,@Stock,@Notas)
SET @iDProducto=@@IDENTITY
SET IDENTITY_INSERT Producto OFF;
这篇关于无法为标识列插入显式值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!