简体   繁体   中英

Return value on stored procedure

I have a problem, when I execute this stored procedure:

IF NOT EXISTS (SELECT * FROM TBL_M 
               WHERE TYPE_M = 'reservation' 
                 AND NUM_DESK = @NUMBER_DESK 
                 AND ID_TIME = @ID_TIME)
BEGIN
    INSERT INTO TBL_M 
    VALUES ('reservation', @NUMBER_DESK, @NUM_USER, @ID_TIME)
END

Everything works correctly, the only problem is that I want to know when the insert is executed the insert and when not.

In C# I use the following method ExecuteNonQuery()

But it always returns -1 , how can you identify the insert and when it doesn't insert.

Declare a variable

Declare @ROWCOUNT INT = 0

Assign the variable

Select @ROWCOUNT = @@ROWCOUNT

Just after the insert (inside begin-end) either return or select (then you have to to use ExecuteQuery instead of ExecuteNonQuery ), or have an output parameter, to get the value of @ROWCOUNT .

@MickyD ' s suggestion:

The SQL variable @@ROWCOUNT hold the number of rows affected by the last statement, in this case just after executing Insert . If any other operation is performed, that value will be not the same any more, hence assign that to a locally defined variable and pass back to the caller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM