从存储过程返回输出

时间:2018-04-16 14:16:14

标签: sql-server tsql stored-procedures

裸体选择我得到了正确的识别

declare @iden table (pk int);

update top (1) lockDate with (UPDLOCK) 
set dt = GETDATE()
output inserted.iden into @iden   
where dt is null 

select top (1) iden.pk
from @iden iden;

当我尝试将其放入存储过程时,我总是得到0.

你知道我做错了吗?

DROP PROCEDURE addLockDate

CREATE PROCEDURE addLockDate
AS  
    DECLARE @iden TABLE (pk INT);

    UPDATE TOP (1) lockDate WITH (UPDLOCK) 
    SET dt = GETDATE()
    OUTPUT inserted.iden INTO @iden   
    WHERE dt IS NULL

    RETURN SELECT TOP (1) iden.pk
           FROM @iden iden
GO

1 个答案:

答案 0 :(得分:0)

未经测试,但这可能有效:

drop PROCEDURE addLockDate
CREATE PROCEDURE addLockDate
AS  
    declare @iden table (pk int);
    update top (1) lockDate with (UPDLOCK) 
       set dt = GETDATE()
    output inserted.iden into @iden   
    where dt is null 
    return (select top (1) iden.pk
    from @iden iden);
GO

这肯定有用:

drop PROCEDURE addLockDate
CREATE PROCEDURE addLockDate
AS  
    declare @iden table (pk int);
    declare @outint int;
    update top (1) lockDate with (UPDLOCK) 
       set dt = GETDATE()
    output inserted.iden into @iden   
    where dt is null 
    set @outint = (select top (1) iden.pk
    from @iden iden);
    return @outint;
GO