[英]SQL Server @@rowcount always returns 0
I have this situation here, this code actually from an old college and he is no longer with me and I'm not very familiar with stored procedure code. 我在这里遇到这种情况,这段代码实际上是来自一所古老的大学,他已经不在我身边了,我对存储过程代码也不是很熟悉。
SELECT @postcode = max(postcode)
FROM user
WHERE uState = @ustate
SELECT @postcode 'POSTCODE'
IF @@rowcount = 0
BEGIN
SELECT @p_status = 8
ROLLBACK TRAN
PRINT 'Unable to obtain new user #. Copy aborted'
RETURN
END
UPDATE address
SET postcode = @postcode
WHERE userID = @userID
When I run the stored procedure, it always returns rowcount = 0, even though there's a value in it. 当我运行存储过程时,即使其中有一个值,它也总是返回rowcount = 0。
How I know there's a value by running below query. 我怎么知道通过运行下面的查询有一个价值。
SELECT MAX(postcode)
FROM user
WHERE uState = @ustate
Please help 请帮忙
Maybe it is a good thing that your colleague left, because the code you showed us seems to have a smell. 您的同事离开也许是一件好事,因为您显示给我们的代码似乎有些臭味。 In particular, I don't know what this line is supposed to be doing:
特别是,我不知道这条线应该做什么:
select @postcode 'POSTCODE'
If you wanted to assign 'POSTCODE'
to @postcode
, then you would use this: 如果您想将
'POSTCODE'
分配给@postcode
,则可以使用以下代码:
select @postcode = 'POSTCODE'
But, the first select is alredy making a possible assignment, so I would just remove this line altogether: 但是,首先选择的是进行可能的分配,因此我将完全删除此行:
SELECT @postcode = max(postcode)
FROM user
WHERE uState = @ustate
IF @@rowcount = 0
BEGIN
SELECT @p_status = 8
ROLLBACK TRAN
PRINT 'Unable to obtain new user #. Copy aborted'
RETURN
END
@@rowcount
should work with select statements, in addition to DML operations, so if that first query returns a value, then the @@rowcount
should not be zero. @@rowcount
除了DML操作外,还应该与select语句一起使用,因此,如果第一个查询返回一个值,则@@rowcount
不应为零。
select @postcode always returns @@rowcount as 1. you can keep row count value in a variable. select @postcode始终将@@ rowcount返回为1.您可以将行计数值保留在变量中。
I think your query should be like that. 我认为您的查询应该是这样的。
DECLARE @RC INT
SELECT @postcode = max(postcode)
FROM user
WHERE uState = @ustate
SET @RC = @@ROWCOUNT
select @postcode 'POSTCODE'
IF @RC = 0
BEGIN
SELECT @p_status = 8
ROLLBACK TRAN
PRINT 'Unable to obtain new user #. Copy aborted'
RETURN
END
UPDATE address
SET postcode = @postcode
WHERE userID = @userID
Just comment this code line " select @postcode 'POSTCODE' " 只需注释此代码行“ select @postcode'POSTCODE'”
Does it have a meaning for the execution of the SP ? SP的执行是否有意义?
-- select @postcode 'POSTCODE'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.