EvilDr
EvilDr

Reputation: 9622

Why is @@rowcount equal to 1 when run in complete isolation?

According to the Microsoft @@ROWCOUNT documentation,

Statements that make a simple assignment always set the @@ROWCOUNT value to 1. No rows are sent to the client. Examples of these statements are: SET @local_variable, RETURN, READTEXT, and select without query statements such as SELECT GETDATE() or SELECT 'Generic Text'.

Somebody else on Stackoverflow also asked why @@rowcount equals 1 when no rows are updated. However, that question used an update statement.

My confusion starts with this example, where no other statements appear:

enter image description here

Reading further into this (Executing if @@rowcount>0 resets @@rowcount to 0. Why?), I then did a second test:

enter image description here

I'm curious to understand please why @@rowcount equals 1 when no other statements are used? Is it because the actual SELECT @@ROWCOUNT statement itself causes an update of @@rowcount?

Upvotes: 2

Views: 611

Answers (1)

George Menoutis
George Menoutis

Reputation: 7240

When running SSMS on windows, I indeed get OP's behaviour. By both doing an inputbuffer check and by using the profiler, it seems that when you open a new query window, ssms performs the following batch:

select @@spid;
select SERVERPROPERTY('ProductLevel');

This results in that default ace.

Upvotes: 4

Related Questions