Saturday, February 25, 2012

Problems with @@ROWCOUNT

Hi,
I am just starting sql and have a stored proc which does a simple select and should return 2 when no rows match the selection criteria . The problem is that it always returns 2, even where there are rows which match the selection criteria and when there are no rows.
CREATE PROCEDURE dbo.SelectSomething
@.a INT,
@.b INT
AS
SET NOCOUNT ON
SELECT a, b, c, d FROM dbo.SomeTable WITH (READUNCOMMITTED)
WHERE a = @.a AND b = @.b
IF @.@.ERROR<>0
RETURN 1
IF @.@.ROWCOUNT=0
RETURN 2 --Always Returns 2
RETURN 0
GO
Thanks for any help.because your condition for @.@.ROWCOUNT is checked against the statement@.@.ERROR which will always be 0 and evaluate to TRUE. You could dosomething like this:
CREATE PROCEDURE dbo.SelectSomething
@.a INT,
@.b INT
AS
SET NOCOUNT ON
DECLARE
@.error int,
@.rowcount int
SELECT
a, b, c, d
FROM
dbo.SomeTable WITH (READUNCOMMITTED)
WHERE
a = @.a AND b = @.b
SELECT
@.error = @.@.ERROR, @.rowcount= @.@.ROWCOUNT
IF @.ERROR<>0
RETURN 1
IF @.ROWCOUNT=0
RETURN 2
RETURN 0
GO

|||

You are right. Thanks very much. I will keep this in mind the next time. Thanks again.

No comments:

Post a Comment