Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How does OUTPUT PARAMETER work with C#, how to understand it correctly?

$
0
0

Hi guys, I'm learning C# with SQL Server.

I am practicing using C# to get a return value

from the output data

of a procedure with output parameter(s).

I write procedure like this

LTER PROCEDURE [dbo].[procAddUser]
    @id VARCHAR(20) ,
    @name NVARCHAR(20) ,
    @des NVARCHAR(50) ,
    @mes VARCHAR(20) OUTPUT
AS
    BEGIN
        IF NOT EXISTS ( SELECT  *
                        FROM    Person.[User]
                        WHERE   id = @id )
            INSERT  Person.[User]
                    ( ID, Name, Description )
            VALUES  ( @id, -- ID - varchar(20)
                      @name, -- Name - nvarchar(20)
                      @des  -- Description - nvarchar(50)
                      )
        ELSE
            SET @mes = N'Already Existed'
    END

and, exec part like this

DECLARE @mess VARCHAR(20);

EXEC dbo.procAddUser @id = 'aa', -- varchar(20)
    @name = N'aa', -- nvarchar(20)
    @des = N'aa', -- nvarchar(50)
    @mes= '' OUTPUT
    
SELECT @mess

But I get error: output can't be used in this situation.

I changed into this way, and it worked.

DECLARE @mess VARCHAR(20);

EXEC dbo.procAddUser @id = 'aa', -- varchar(20)
    @name = N'aa', -- nvarchar(20)
    @des = N'aa', -- nvarchar(50)
    @mes= @mess OUTPUT
    
SELECT @mess

I can't understand this part:

@mes = @mess output

In my thought, I think that the value of @mess was passed to @mes.

So, I could only get NULL when use "select @mess"

Please help...

Also, I want to use the output value of @mes in C# code.

Is there some manual about this? I just couldn't find where to find them.

Thank you for help...


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>