Home Forums Programming General OUTPUT parameter - I understood its meaning correctly?! RE: OUTPUT parameter - I understood its meaning correctly?!

  • nhatnghe39 - Wednesday, April 25, 2018 12:41 AM

    Well, I only wonder why we can assign @MyIdent to @OrderID right away after declaring it, maybe I will assume that @OrderID passed out from sproc to execute, when run this line:


    @OrderID = @MyIdent OUTPUT

    SQL Server will assign value from @OrderID to @MyIdent...

    Before calling proc
    1 - Value of @MyIdent is assigned to @OrderID, and is available for use in the proc.  As it is an OUTPUT param, it can be changed by the proc.

    In Proc ...
    1 - INSERT into table
    2 - Table IDENTITY increases
    3 - @@IDENTITY is assigned latest IDENTITY value
    4 - @@IDENTITY value assigned to @OrderID, and passed out

    After calling proc
    1 - Value of @OrderID is assigned to @MyIdent

    Below is a temp proc that shows how the value can be used, and changed.
    CREATE PROCEDURE #TestOutParam
    @ParamVal INT OUTPUT
    AS
    BEGIN
    SELECT InputParamVal = @ParamVal;
    SET @ParamVal += 10;
    SELECT OutputParamVal = @ParamVal;
    END;
    GO

    DECLARE @MyVal INT = 5;

    EXEC #TestOutParam @ParamVal = @MyVal OUTPUT;

    SELECT MyVal = @MyVal;
    GO

    DROP PROCEDURE dbo.#TestOutParam;
    GO