SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Rowcount and multiple assignment


Rowcount and multiple assignment

Author
Message
honza.mf
honza.mf
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1949 Visits: 1323
Comments posted to this topic are about the item Rowcount and multiple assignment



See, understand, learn, try, use efficient
© Dr.Plch
Oleg Netchaev
Oleg Netchaev
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1785 Visits: 1814
This is a great question, I really, really enjoyed it. The most difficult part was to figure out that the line:

set @result = cast (@@rowcount as varchar)


does 2 things:

1. it sets the value of the @result variable to 1 because @@rowcount was equal to 1 as a result of the last insert
2. it resets @@rowcount to 1 as a result of @result assignment.

The @@rowcount then stays unchanged (still equal to 1) because the

select @result = @result + cast (@@rowcount as varchar) from #n; 


which pads the @result has the @@rowcount inside of it, but the moment the statement bails out the @@rowcount is then reset to whatever the number of affected records was, which happens to be 3.

The commented line reading set nocount off; was there just for demonstration purposes I suppose, meaning that it could be any other set statement, such as set ansi_nulls on; or set transaction isolation level read uncommitted; or whatever other set. Any such set statement when uncommented would just reset the @@rowcount to 0 changing the final answer to 10003 from 11113.

I really hope that what I figured is correct, and my answer was not a result of some lucky guess.

Thanks again.

Oleg
honza.mf
honza.mf
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1949 Visits: 1323
Oleg Netchaev (4/13/2010)
This is a great question, I really, really enjoyed it. The most difficult part was to figure out that the line:

set @result = cast (@@rowcount as varchar)


does 2 things:

1. it sets the value of the @result variable to 1 because @@rowcount was equal to 1 as a result of the last insert
2. it resets @@rowcount to 1 as a result of @result assignment.

The @@rowcount then stays unchanged (still equal to 1) because the

select @result = @result + cast (@@rowcount as varchar) from #n; 


which pads the @result has the @@rowcount inside of it, but the moment the statement bails out the @@rowcount is then reset to whatever the number of affected records was, which happens to be 3.

The commented line reading set nocount off; was there just for demonstration purposes I suppose, meaning that it could be any other set statement, such as set ansi_nulls on; or set transaction isolation level read uncommitted; or whatever other set. Any such set statement when uncommented would just reset the @@rowcount to 0 changing the final answer to 10003 from 11113.

I really hope that what I figured is correct, and my answer was not a result of some lucky guess.

Thanks again.

Oleg



Thank you.
My interest writing the script was the behaviour of @@rowcount during mulitple assignment in single select.

Yes, you are right, the commented set statement is there just to be uncommented to show the change of the result.

You figured it very well.



See, understand, learn, try, use efficient
© Dr.Plch
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9023 Visits: 7283
Well thought out question, well done.

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Rune Bivrin
Rune Bivrin
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3910 Visits: 1502
It's important to note that SET @result = cast (@@rowcount as varchar) does NOT change @@ROWCOUNT. The first 1 comes from the last INSERT #n VALUES(3).

This is one of the important differences between SELECT and SET when assigning variables. SET never yields a rowcount, and thus doesn't change @@ROWCOUNT.


Just because you're right doesn't mean everybody else is wrong.
ronmoses
ronmoses
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1241 Visits: 996
I'm not ashamed to admit that I've read this problem and the solution several times, as well as the corresponding BOL topics, and I still can't make heads or tails of it. This question has easily made me feel more thick-headed than any other I've seen here, so well done, Honza! Ultimately I had to pick an answer at random just so I could get to the forum thread, where hopefully my head will be pulled from my a** with a resounding POP! :-D

-----
a haiku...

NULL is not zero
NULL is not an empty string
NULL is the unknown

honza.mf
honza.mf
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1949 Visits: 1323
Sorry, ronmoses, for your headache.
I was curious about one thing, and I decided to make it a QotD, I mixed several things together.
To tell the truth, greatest work was to write all the answers :-)



See, understand, learn, try, use efficient
© Dr.Plch
ronmoses
ronmoses
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1241 Visits: 996
honza.mf (4/14/2010)
Sorry, ronmoses, for your headache.

Oh, no need for an apology, my post was intended as a compliment! Most of the questions I see here are either things I can easily get my head around (whether I know the answer or not) or things that are way outside the realm of my current experience level. This question should have been the former, and I'm kinda happy to be struggling with it. So thanks for the great question!

-----
a haiku...

NULL is not zero
NULL is not an empty string
NULL is the unknown

Tom Garth
Tom Garth
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1261 Visits: 1499
That was fun and surprising. I didn't notice the quirky update until I ran it.

Thanks for a good QotD.

Rune Bivrin (4/14/2010)
It's important to note that SET @result = cast (@@rowcount as varchar) does NOT change @@ROWCOUNT. The first 1 comes from the last INSERT #n VALUES(3).

This is one of the important differences between SELECT and SET when assigning variables. SET never yields a rowcount, and thus doesn't change @@ROWCOUNT.

That does not seem to be true. @@ROWCOUNT yields the result of the last command. SET yields 1 whereas PRINT yields 0.
declare @n varchar(5)

select @n = @@rowcount
/* Row count is 1 after SELECT */
print @@rowcount
/* Row count is 0 after PRINT */
print @@rowcount

set @n = @@rowcount
/* Row count is 1 after SET */
print @@rowcount



Tom Garth
Vertical Solutions

"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers

honza.mf
honza.mf
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1949 Visits: 1323
Tom Garth (4/14/2010)
That was fun and surprising. I didn't notice the quirky update until I ran it.

Thanks for a good QotD.

Rune Bivrin (4/14/2010)
It's important to note that SET @result = cast (@@rowcount as varchar) does NOT change @@ROWCOUNT. The first 1 comes from the last INSERT #n VALUES(3).

This is one of the important differences between SELECT and SET when assigning variables. SET never yields a rowcount, and thus doesn't change @@ROWCOUNT.

That does not seem to be true. @@ROWCOUNT yields the result of the last command. SET yields 1 whereas PRINT yields 0.
declare @n varchar(5)

select @n = @@rowcount
/* Row count is 1 after SELECT */
print @@rowcount
/* Row count is 0 after PRINT */
print @@rowcount

set @n = @@rowcount
/* Row count is 1 after SET */
print @@rowcount



As said in BOL:
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'.

Statements that make an assignment in a query or use RETURN in a query set the @@ROWCOUNT value to the number of rows affected or read by the query, for example: SELECT @local_variable = c1 FROM t1.

Data manipulation language (DML) statements set the @@ROWCOUNT value to the number of rows affected by the query and return that value to the client. The DML statements may not send any rows to the client.

DECLARE CURSOR and FETCH set the @@ROWCOUNT value to 1.

EXECUTE statements preserve the previous @@ROWCOUNT.

Statements such as USE, SET <option>, DEALLOCATE CURSOR, CLOSE CURSOR, BEGIN TRANSACTION or COMMIT TRANSACTION reset the ROWCOUNT value to 0.


But there is something strange as "SELECT GETDATE()" returns a row to the client. Author probably omitted some "@d=". It happens.



See, understand, learn, try, use efficient
© Dr.Plch
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search