SQL Server output parameter issue

后端 未结 8 1646
南旧
南旧 2020-12-24 11:42

I am using SQL Server 2008 Enterprise. I am learning OUTPUT parameter of SQL Server stored procedure. For example, stored procedure sp_add_jobschedule has an OUTPUT paramete

8条回答
  •  生来不讨喜
    2020-12-24 12:44

    Two other things I think that are worth noting:

    1) You can pass more than one parameter as OUTPUT,

    2) You do not have to call the parameters with OUTPUT if you don't want the results

    CREATE PROCEDURE ManyOutputs @a int, @b int output, @c varchar(100) output, @d bit output
    AS
    BEGIN
        SET @b = @a + 11
        SET @c = 'The Value of A is ' + CAST(@a AS varchar(5)) + ', and the value of B is ' + CAST(@b AS varchar(5))
        IF (@a % 2 = 1)
            SET @d = 1
        ELSE
            SET @d = 0
    END
    GO
    

    Calling this routine:

    DECLARE @bVal int
    DECLARE @cVal varchar(100)
    DECLARE @dVal bit
    
    EXEC ManyOutputs 1, @bVal, @cVal, @dVal
    SELECT @bVal AS bVal, @cVal as cVal, @dVal as dVal
    

    Returns NULL, NULL, NULL

    EXEC ManyOutputs 2, @bVal OUT, @cVal OUT, @dVal OUT
    SELECT @bVal AS bVal, @cVal as cVal, @dVal as dVal
    

    Returns 13, "The Value of A is 2, and the value of B is 13", 0

    EXEC ManyOutputs 3, @bVal, @cVal, @dVal
    SELECT @bVal AS bVal, @cVal as cVal, @dVal as dVal
    

    Returns 13, "The Value of A is 2, and the value of B is 13", 0

    (the same as the last call, because we didn't get new values by using OUTPUT, so it retained the old values.)

    EXEC ManyOutputs 5, @bVal OUT, @cVal OUT, @dVal OUT
    SELECT @bVal AS bVal, @cVal as cVal, @dVal as dVal
    

    Returns 16, "The Value of A is 5, and the value of B is 16", 1

提交回复
热议问题