SQL Server output parameter issue

后端 未结 8 1642
南旧
南旧 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:46

    Adding further to what others have said above, OUTPUT parameters can act as INPUT as well as OUTPUT. But, it depends on the stored procedure to use the value passed to it.

    If the stored procedures ignores the value passed in for OUTPUT parameter (which it generally should), the INPUT value gets ignored anyway.

    Using Sergey's code, I can use the value user passed for @intOutput (if I need to)

    create PROCEDURE test_proc
    
    @intInput int,
    @intOutput int OUTPUT
    
    AS
    set @intOutput = @intOutput + 1 
    
    go
    

    But, that defeats the purpose of OUTPUT parameter.
    To give a different view, c# compiler forces you to overwrite the out parameter's value by assignment (without using the output parameter).

    e.g. I cannot do this in c#. Here it acts as only out parameter (i.e ignore the value user passed to this function)

    static void dosomething(out int i)
    {
        //i = 0;
        i = i + 1;
    }
    

提交回复
热议问题