Scope_Identity()始终返回0

时间:2016-03-29 01:05:17

标签: sql sql-server tsql scope-identity

以下存储过程正确插入新行,但始终为输出参数返回0。什么会返回新的行ID?

alter procedure DlogErr 
    @Msg varchar(100), @Cmd nvarchar(max), 
    @Stk varchar(200), @Pgm varchar(50), 
    @Lin smallint, @EID int, @UID int output 
as
begin
    set nocount on

    insert into ErrorLog (Src, ErrMsg, Cmd, StackInfo, Itm, Pgm, Lin, ErrID) 
    values ('VFP', @Msg, @Cmd, @Stk, '', @Pgm, @Lin, @EID);

    set @UID = Scope_Identity();
end

1 个答案:

答案 0 :(得分:0)

最有可能的是,在调用存储过程时,您可能忘记附加out关键字:

declare @varUID int
exec DlogErr @Msg='abc', @Cmd = 'foo', @Stk ='', @Pgm = '', @Lin = 0, @EID = 0 , @UID =  @varUID out
select @varUID