|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 6:14 AM
Points: 1,102,
Visits: 1,197
|
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Yesterday @ 10:53 AM
Points: 1,662,
Visits: 1,709
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 6:14 AM
Points: 1,102,
Visits: 1,197
|
|
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
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: 2 days ago @ 3:48 AM
Points: 3,125,
Visits: 4,311
|
|
Well thought out question, well done.
____________________________________________ Space, the final frontier? not any more... All limits henceforth are self-imposed. “libera tute vulgaris ex”
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 12:46 AM
Points: 2,657,
Visits: 719
|
|
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.
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Yesterday @ 5:12 AM
Points: 649,
Visits: 686
|
|
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!
----- a haiku...
NULL is not zero NULL is not an empty string NULL is the unknown
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 6:14 AM
Points: 1,102,
Visits: 1,197
|
|
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
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Yesterday @ 5:12 AM
Points: 649,
Visits: 686
|
|
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
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, February 04, 2011 7:20 AM
Points: 977,
Visits: 1,499
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 6:14 AM
Points: 1,102,
Visits: 1,197
|
|
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
|
|
|
|