How to return the output of stored procedure into a variable in sql server

前端 未结 4 1973
予麋鹿
予麋鹿 2020-11-28 06:26

I want to execute a stored procedure in SQL Server and assign the output to a variable (it returns a single value) ?

4条回答
  •  情话喂你
    2020-11-28 07:06

    Use this code, Working properly

    CREATE PROCEDURE [dbo].[sp_delete_item]
    @ItemId int = 0
    @status bit OUT
    
    AS
    Begin
     DECLARE @cnt int;
     DECLARE @status int =0;
     SET NOCOUNT OFF
     SELECT @cnt =COUNT(Id) from ItemTransaction where ItemId = @ItemId
     if(@cnt = 1)
       Begin
         return @status;
       End
     else
      Begin
       SET @status =1;
        return @status;
     End
    END
    

    Execute SP

    DECLARE @statuss bit;
    EXECUTE  [dbo].[sp_delete_item] 6, @statuss output;
    PRINT @statuss;
    

提交回复
热议问题