@@RowCount return 1 even the previous select condition was False

EagerToLearn

I have this simple code

declare @a int, @b int 
select @a=sum(1), @b=1 from syscolumns where 1 = 2
select @@rowcount, @a, @b

The result is : @@rowcount=1, @a=null, @b=1

@a = null is fine to me, but how come @@rowcount and @b=1

SQLFiddle : https://rextester.com/PKCB94229

Martin Smith

This is because scalar aggregates (no GROUP BY) always return one row even on an empty input (assuming no HAVING clause eliminates it)

select sum(1) from syscolumns where 1 = 2 returns a resultset with one row, so @@ROWCOUNT is correctly reported as 1.

If you want to count the input rows from a query that assigns scalar aggregates to variables then assigning COUNT(*) to another variable would be simplest.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

'And' condition evaluated even if previous condition is false in VBA

Return from select condition true or false

.isAfter return even false

if statement executing even if condition is false

If statement executed even if the condition is false

condition is met even when is false

If/Then - then executing even when condition is false

if statements execute even with false condition

This condition will always return 'false' since the types '1' and '2' have no overlap

How to select the previous date with a condition

Return false if no rows for select

Why does this function return "None" if the number isn't even? How can I make it return "False" without using an else condition?

Reserve space for DIV even if the condition is false

Always include children even if condition is false

The condition is TRUE even though it should be FALSE

Code execute even if where condition is false

MSBuild builds target even though condition is false

Issue with if function executing even if the condition is false

for loop is executing even though the condition should be false

False positive PDO rowcount

SELECT into a foreach return previous values

TestCafe test script checkbox.checked always return false even when checked, how can I check the checkbox state in an if-condition?

Return records from table 1 left join table 2 even if where condition fails

React array const is storing false values even when condition is false

Return values from previous rows based on condition

PHP False return even after it is true

IsInRole return false even if there is role in claims

javascript submits form even if return false

jQuery refreshes page even with return false