[英]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()
在 C# 我使用以下方法ExecuteNonQuery()
But it always returns -1
, how can you identify the insert and when it doesn't insert.但它总是返回-1
,您如何识别插入以及何时不插入。
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
.就在插入(开始-结束内部)之后, return
或select
(然后你必须使用ExecuteQuery
而不是ExecuteNonQuery
),或者有一个 output 参数,以获取@ROWCOUNT
的值。
@MickyD ' s suggestion: @MickyD 的建议:
The SQL variable @@ROWCOUNT
hold the number of rows affected by the last statement, in this case just after executing Insert
. SQL 变量@@ROWCOUNT
保存受最后一条语句影响的行数,在这种情况下,就在执行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.如果执行任何其他操作,则该值将不再相同,因此将其分配给本地定义的变量并传回给调用者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.