Posts

Showing posts from December, 2009

SQL Server @@ROWCOUNT returns 1 if Min Max used in query

Hi,

I came across this crazy issue today. You expect @@Rowcount to return number of rows returned by query right. But in my case it was returning 1 for 0 rows returned.
Here is an example.

SELECT *
FROM yourtable WHERE 1 = 2

SELECT @@ROWCOUNT -- THIS GIVES 0

But if you have MIN function being used, the result will be different

declare @company_id int

SELECT @company_id = min(company_id)
FROM yourtable WHERE 1 = 2

SELECT @@ROWCOUNT -- THIS GIVES 1

Apparently, since NULL was the result set, @@rowcount was giving 1 as the count.

I hope this helps someone.