我正在进行级联插入,其中存储过程应该返回插入行的id。如果表的id是int,这不会成为问题。但是,ID为varchar
,因此我无法使用SCOPE_IDENTITY()
。
到目前为止,这是我的程序:
CREATE PROCEDURE NEW_ARTICLE
@id varchar(50) OUTPUT,
@name varchar(100),
@articleNr varchar(50),
@gategory varchar(50),
@containerId varchar(50),
@contPerContainer int,
@pictureId varchar(50)
AS
SET NOCOUNT OFF
INSERT INTO nextlabel.[Article] (name, article_nr, category, container_id, count_per_container, picture_id )
VALUES (@name, @articleNr, @gategory, @containerId, @contPerContainer, @pictureId)
SET @id = SCOPE_IDENTITY()
最后一行不正确,因为id
列是varchar
。
如何退回id
?
答案 0 :(得分:3)
试试这个:
CREATE PROCEDURE NEW_ARTICLE
@id varchar(50) OUTPUT,
@name varchar(100),
@articleNr varchar(50),
@gategory varchar(50),
@containerId varchar(50),
@contPerContainer int,
@pictureId varchar(50)
AS
SET NOCOUNT OFF
SET @id = newid()
INSERT INTO nextlabel.[Article] (id, name, article_nr, category, container_id, count_per_container, picture_id)
VALUES (@id, @name, @articleNr, @gategory, @containerId, @contPerContainer, @pictureId)
GO