简体   繁体   中英

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:

在此处输入图片说明

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

在此处输入图片说明

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM