Set a SP return value to a variable in SQL Server

前端 未结 3 2058
长发绾君心
长发绾君心 2021-02-05 15:45

I have a sproc that returns a single line and column with a text, I need to set this text to a variable, something like:

declare @bla varchar(100)
select @bla =          


        
3条回答
  •  眼角桃花
    2021-02-05 16:31

    If the stored procedure is returning a single value you could define one of the parameters on the stored procedure to be an OUTPUT variable, and then the stored procedure would set the value of the parameter

    CREATE PROCEDURE dbo.sp_Name
        @In INT,
        @Out VARCHAR(100) OUTPUT
    
    AS
    BEGIN
        SELECT @Out = 'Test'
    END
    GO
    

    And then, you get the output value as follows

    DECLARE @OUT VARCHAR(100)
    EXEC sp_name 1, @Out OUTPUT
    PRINT @Out
    

提交回复
热议问题