Use RowCount to determine query success

If you have a Stored Procedure that populates a temporary table, and you want to ensure the temporary table has been populated with something, you could use @@Rowcount.

It’s an in-build property, and can be used as an alternative to selecting COUNT(1) from the temporary table.

Here is an example

CREATE TABLE #Users
(
  UserID INT,
  Username NVARCHAR(100)
)

INSERT INTO
  #Users
SELECT
  UserID,
  Username
FROM
  User
WHERE
  Locked = 0

IF @@ROWCOUNT = 0
BEGIN
  SELECT 'No valid users available'
END

I hope that helps!

Subscribe

0 comments