|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Monday, February 25, 2013 12:52 PM
Points: 513,
Visits: 426
|
|
Well, I thought I'd @@ROWCOUNT in the bag until this came along  Nice question.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 6:14 AM
Points: 1,102,
Visits: 1,197
|
|
rjv_rnjn (4/14/2010)
Well, I thought I'd @@ROWCOUNT in the bag until this came along  Nice question. Everytime I thought I'd something in the bag I realized I have overlooked some detail. Very often it's a very important detail
See, understand, learn, try, use efficient © Dr.Plch
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, January 29, 2013 4:08 AM
Points: 1,140,
Visits: 306
|
|
I enjoyed working this one out, line by line :)
Good little puzzle to start the day. Thanks!
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, January 29, 2013 4:08 AM
Points: 1,140,
Visits: 306
|
|
? ... from 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'."
... and ..
"Statements such as USE, SET <option>, DEALLOCATE CURSOR, CLOSE CURSOR, BEGIN TRANSACTION or COMMIT TRANSACTION reset the ROWCOUNT value to 0."
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, April 03, 2013 7:27 AM
Points: 40,
Visits: 67
|
|
| I spend 30 precious minutes every Friday reading my SQL Server Central articles and attempting the QotD. This is the first time I cheated (IMHO) and ran the code for an answer. Like other people, the 111 was a surprise to me and is highly useful esoteric knowledge for future use... In short - excellent QotD!
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:48 PM
Points: 7,088,
Visits: 7,143
|
|
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.
Someone may already have said this, but in case they haven't:
That is very wrong (but it doesn't make a difference to the answer in this case because a SET @localvariable statement sets @@rowcount to 1).
From BoL (http://msdn.microsoft.com/en-gb/library/ms187316.aspx): 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'.
This can be demonstrated by a small adaptation to the code of the question:declare @result varchar (5) create table #n (n int) insert #n values (1),(2),(2) set @result = cast (@@rowcount as varchar) /*SET NOCOUNT OFF*/ select @result = @result + cast (@@rowcount as varchar) from #n select @result + cast (@@rowcount as varchar) drop table #n which will deliver 31113, not 33333 as would be the case if SET did not set @@rowcount.
Tom Que conclure à la fin de tous mes longs propos? C'est que les préjugés sont la raison des sots. (Voltaire, 1756)
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 5:01 PM
Points: 10,989,
Visits: 10,531
|
|
Tom.Thomson (4/17/2010) Someone may already have said this, but in case they haven't: Actually, pretty much every other post so far has covered it and Rune responded: "Yep, I was wrong about that. And I even ran a test to verify my thought before I posted, but I managed to read the results of that test wrong. My bad. I suppose that was because I expected it to work that way simply based on the fact that SET doesn't yield a 1 row(s) affected." Ah, well. Learn something new everyday.
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:48 PM
Points: 7,088,
Visits: 7,143
|
|
Paul White NZ (4/18/2010)
Tom.Thomson (4/17/2010) Someone may already have said this, but in case they haven't:Actually, pretty much every other post so far has covered it  and Rune responded: "Yep, I was wrong about that. And I even ran a test to verify my thought before I posted, but I managed to read the results of that test wrong. My bad. I suppose that was because I expected it to work that way simply based on the fact that SET doesn't yield a 1 row(s) affected." Ah, well. Learn something new everyday. Yes, if I had read all the posts before firing up the editor I wouldn't have bothered.
Tom Que conclure à la fin de tous mes longs propos? C'est que les préjugés sont la raison des sots. (Voltaire, 1756)
|
|
|
|