存储过程错误,要求为输出传递id

时间:2014-02-16 06:09:58

标签: sql sql-server stored-procedures

我有这个插入语句

CREATE PROCEDURE s_Add$Subject 
  (@SubjectName varchar(50),
  @TheSubjectID int OUT)
AS
  INSERT INTO [Subject] (SubjectName)
    VALUES (@SubjectName)
  SET @TheSubjectID = @@IDENTITY

我不记得曾经发生过这种情况,但由于输出参数是输出,为什么要调用id来传递?然后我尝试将@TheSubjectID作为声明的变量并且它抛出一个错误,我将其设置为

declare @TheSubjectID int out

它导致错误,因为它说int的语法不正确。哦,请不要抨击我使用@@IDENTITY而不是SCOPE_IDENTITY,我更喜欢@@IDENTITY,并且从未遇到过任何问题。 :)

1 个答案:

答案 0 :(得分:0)

试试这个

CREATE PROCEDURE s_Add$Subject (
   @SubjectName varchar(50), 
   @TheSubjectID int = NULL OUT)
AS
  INSERT INTO [Subject] (SubjectName)
    VALUES (@SubjectName)
  SET @TheSubjectID = @@IDENTITY