﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Honza Mensdorff  / Rowcount and multiple assignment / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 22 May 2013 20:24:50 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>[quote][b]Paul White NZ (4/18/2010)[/b][hr][quote][b]Tom.Thomson (4/17/2010)[/b][hr]Someone may already have said this, but in case they haven't:[/quote]Actually, pretty much every other post so far has covered it :w00t: and Rune responded:[i]"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.[/i][/quote]Yes, if I had read all the posts before firing up the editor I wouldn't have bothered.</description><pubDate>Sun, 18 Apr 2010 09:55:45 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>[quote][b]Tom.Thomson (4/17/2010)[/b][hr]Someone may already have said this, but in case they haven't:[/quote]Actually, pretty much every other post so far has covered it :w00t: and Rune responded:[i]"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.[/i]</description><pubDate>Sun, 18 Apr 2010 05:06:33 GMT</pubDate><dc:creator>Paul White</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>[quote][b]Rune Bivrin (4/14/2010)[/b][hr]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.[/quote]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 ([url]http://msdn.microsoft.com/en-gb/library/ms187316.aspx[/url]): [quote]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'. [/quote]This can be demonstrated by a small adaptation to the code of the question:[code]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 #nselect @result + cast (@@rowcount as varchar)drop table #n[/code]which will deliver 31113, not 33333 as would be the case if SET did not set @@rowcount.</description><pubDate>Sat, 17 Apr 2010 05:30:00 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>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 - [b]excellent QotD![/b]</description><pubDate>Fri, 16 Apr 2010 08:59:58 GMT</pubDate><dc:creator>alitchfield</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>? ... 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 &amp;lt;option&amp;gt;, DEALLOCATE CURSOR, CLOSE CURSOR, BEGIN TRANSACTION or COMMIT TRANSACTION reset the ROWCOUNT value to 0."</description><pubDate>Thu, 15 Apr 2010 01:56:21 GMT</pubDate><dc:creator>Craig@Work</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>I enjoyed working this one out, line by line :)Good little puzzle to start the day. Thanks!</description><pubDate>Thu, 15 Apr 2010 01:54:33 GMT</pubDate><dc:creator>Craig@Work</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>[quote][b]rjv_rnjn (4/14/2010)[/b][hr]Well, I thought I'd @@ROWCOUNT in the bag until this came along :-PNice question.[/quote]Everytime I thought I'd something in the bag I realized I have overlooked some detail. Very often it's a very important detail:-D</description><pubDate>Thu, 15 Apr 2010 01:20:25 GMT</pubDate><dc:creator>honza.mf</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>Well, I thought I'd @@ROWCOUNT in the bag until this came along :-PNice question.</description><pubDate>Wed, 14 Apr 2010 14:49:23 GMT</pubDate><dc:creator>rjv_rnjn</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>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 [i]1 row(s) affected[/i].Ah, well. Learn something new everyday.</description><pubDate>Wed, 14 Apr 2010 13:32:35 GMT</pubDate><dc:creator>Rune Bivrin</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>Great question. I got it wrong - I correctly followed the logic of the script but had too little understanding of how ROWCOUNT gets set. Good stuff to know (and study!).Thanks,webrunner</description><pubDate>Wed, 14 Apr 2010 12:24:49 GMT</pubDate><dc:creator>webrunner</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>Good question.  Made me realize that I need to study @@rowcount a bit more.</description><pubDate>Wed, 14 Apr 2010 10:58:24 GMT</pubDate><dc:creator>Trey Staker</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>Nice question.  It made me think and the explanation was good too.</description><pubDate>Wed, 14 Apr 2010 10:27:49 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>[quote][b]sjimmo (4/14/2010)[/b][hr]Oleg,insert into #nselect         top 3 row_number() over (order by [object_id])        from sys.objects;is not the same as insert #n values (1)insert #n values (2)insert #n values (3)Should you check the @@rowcount after performing your select, it will return 3, meaning that there were 3 rows affected by your SQL call.In the individual inserts,it will return a 1 after the last insert command.[/quote]Yes, this is precisely what I tried to point out. In the original script the last insert as well as any insert before that set the @@rowcount to 1, and then the line set @result = cast (@@rowcount as varchar);did 2 things (set the variable and then reset the @@rowcount back to 1. So I changed the insert simply to reveal this behavior. The set-based insert set the @@rowcount to 3 and the line set @result = cast (@@rowcount as varchar); sets the @@rowcount to one as a result of the variable set.Oleg</description><pubDate>Wed, 14 Apr 2010 09:41:29 GMT</pubDate><dc:creator>Oleg Netchaev</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>Oleg,insert into #nselect         top 3 row_number() over (order by [object_id])        from sys.objects;is not the same as insert #n values (1)insert #n values (2)insert #n values (3)Should you check the @@rowcount after performing your select, it will return 3, meaning that there were 3 rows affected by your SQL call.In the individual inserts,it will return a 1 after the last insert command.@@ROWCOUNT changes after each SQL call, which is why it needs to be immediately after any command which you want to check the rowcont of.@@ROWCOUNT returns the specific number of rows either returned from a query or affected by a transaction. The value of @@ROWCOUNT is ALWAYS the value of the immediate preceeding SQL call. Thus, your example is selecting 3 rows and inserting them as a batch. The QOD is performing 3 seperate inserts.</description><pubDate>Wed, 14 Apr 2010 09:26:17 GMT</pubDate><dc:creator>sjimmo</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>This is a nice example of why you SHOULD NOT test @@ROWCOUNT at the start of a trigger and exit if the value is zero.  Although this technique has been documented in independent books (e.g., Ken Henderson) the value could be zero from some other action performed by some other trigger or program unit (procedure, function).  I.e., the value of @@ROWCOUNT does not always reflect the number of rows affected in the table that the trigger is attached to when the trigger is fired.</description><pubDate>Wed, 14 Apr 2010 09:07:02 GMT</pubDate><dc:creator>Mauve</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>[quote][b]Rune Bivrin (4/14/2010)[/b][hr]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.[/quote]I am sorry, but this is just plain wrong. Here is the proof showing that when I said that the line [code="sql"]set @result = cast (@@rowcount as varchar);[/code]does 2 things (sets the value of @result to 1 because @@rowset was equal to 1 as a result of the last insert, and then resets the value of @@rowcount back to one). Lets change the original script to substitute 3 inserts (each inserting one record) with a single insert inserting 3 records in one set. If your statement about SET never yields a rowcount is correct then we should see the final answer as 33333, but the result is actually going to be 31113 insead.[code="sql"]declare @result varchar (5);create table #n (n int);insert into #nselect 	top 3 row_number() over (order by [object_id])	from sys.objects;-- @@rowcount is equal to 3 after the insert, and @@rowcount is equal --to 1 after the set below is executed, so set @result = ... does indeed --sets the rowcount. The confusion comes from the fact that the statements -- such as set ansi_nulls, quoted_identifier or whatever other set do not-- have so-called rows affected so those set statements reset the @@rowcount -- to 0, that is all. They still change the value of the @@rowcount though :)set @result = cast (@@rowcount as varchar);select @result = @result + cast (@@rowcount as varchar) from #n; select @result + cast (@@rowcount as varchar);[/code]The above yields 31113 not 33333Oleg </description><pubDate>Wed, 14 Apr 2010 09:03:36 GMT</pubDate><dc:creator>Oleg Netchaev</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>[quote][b]Rune Bivrin (4/14/2010)[/b][hr]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.[/quote]Are you sure about this? I tried the following on my server and did not get a result consistent with your statement above. I am using SQL 2005declare @result intselect 1 union select 2set @result = @@rowcountselect @@rowcountI got -----------12-----------1</description><pubDate>Wed, 14 Apr 2010 09:00:33 GMT</pubDate><dc:creator>kevin.l.williams</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>Very cool QotD -- thanks!</description><pubDate>Wed, 14 Apr 2010 08:50:09 GMT</pubDate><dc:creator>Michael Poppers</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>[quote][b]Tom Garth (4/14/2010)[/b][hr]That was fun and surprising. I didn't notice the quirky update until I ran it.Thanks for a good QotD.[quote][b]Rune Bivrin (4/14/2010)[/b][hr]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.[/quote]That does not seem to be true. @@ROWCOUNT yields the result of the last command. SET yields 1 whereas PRINT yields 0.[code="sql"]declare @n varchar(5)select @n = @@rowcount/*	Row count is 1 after SELECT	*/	print @@rowcount/*	Row count is 0 after PRINT	*/	print @@rowcountset @n = @@rowcount/*	Row count is 1 after SET	*/	print @@rowcount[/code][/quote]As said in BOL:[quote]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 &amp;lt;option&amp;gt;, DEALLOCATE CURSOR, CLOSE CURSOR, BEGIN TRANSACTION or COMMIT TRANSACTION reset the ROWCOUNT value to 0.[/quote]But there is something strange as "SELECT GETDATE()" returns a row to the client. Author probably omitted some "@d=". It happens.</description><pubDate>Wed, 14 Apr 2010 08:05:57 GMT</pubDate><dc:creator>honza.mf</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>That was fun and surprising. I didn't notice the quirky update until I ran it.Thanks for a good QotD.[quote][b]Rune Bivrin (4/14/2010)[/b][hr]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.[/quote]That does not seem to be true. @@ROWCOUNT yields the result of the last command. SET yields 1 whereas PRINT yields 0.[code="sql"]declare @n varchar(5)select @n = @@rowcount/*	Row count is 1 after SELECT	*/	print @@rowcount/*	Row count is 0 after PRINT	*/	print @@rowcountset @n = @@rowcount/*	Row count is 1 after SET	*/	print @@rowcount[/code]</description><pubDate>Wed, 14 Apr 2010 07:50:33 GMT</pubDate><dc:creator>Tom Garth</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>[quote][b]honza.mf (4/14/2010)[/b][hr]Sorry, ronmoses, for your headache.[/quote]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 [i]way[/i] 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!</description><pubDate>Wed, 14 Apr 2010 06:52:55 GMT</pubDate><dc:creator>ronmoses</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>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 :-)</description><pubDate>Wed, 14 Apr 2010 06:42:36 GMT</pubDate><dc:creator>honza.mf</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>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</description><pubDate>Wed, 14 Apr 2010 06:14:22 GMT</pubDate><dc:creator>ronmoses</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>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.</description><pubDate>Wed, 14 Apr 2010 03:23:26 GMT</pubDate><dc:creator>Rune Bivrin</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>Well thought out question, well done.</description><pubDate>Wed, 14 Apr 2010 02:01:56 GMT</pubDate><dc:creator>Stewart "Arturius" Campbell</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>[quote][b]Oleg Netchaev (4/13/2010)[/b][hr]This is a great question, I really, really enjoyed it. The most difficult part was to figure out that the line:[code="sql"]set @result = cast (@@rowcount as varchar)[/code]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 insert2. it resets @@rowcount to 1 as a result of @result assignment. The @@rowcount then stays unchanged (still equal to 1) because the [code="sql"]select @result = @result + cast (@@rowcount as varchar) from #n; [/code]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 [b]set nocount off;[/b] was there just for demonstration purposes I suppose, meaning that it could be any other set statement, such as [b]set ansi_nulls on;[/b] or [b]set transaction isolation level read uncommitted;[/b] or [b][i]whatever other set[/i][/b]. 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[/quote]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.</description><pubDate>Wed, 14 Apr 2010 00:15:57 GMT</pubDate><dc:creator>honza.mf</dc:creator></item><item><title>RE: Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>This is a great question, I really, really enjoyed it. The most difficult part was to figure out that the line:[code="sql"]set @result = cast (@@rowcount as varchar)[/code]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 insert2. it resets @@rowcount to 1 as a result of @result assignment. The @@rowcount then stays unchanged (still equal to 1) because the [code="sql"]select @result = @result + cast (@@rowcount as varchar) from #n; [/code]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 [b]set nocount off;[/b] was there just for demonstration purposes I suppose, meaning that it could be any other set statement, such as [b]set ansi_nulls on;[/b] or [b]set transaction isolation level read uncommitted;[/b] or [b][i]whatever other set[/i][/b]. 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</description><pubDate>Tue, 13 Apr 2010 21:50:17 GMT</pubDate><dc:creator>Oleg Netchaev</dc:creator></item><item><title>Rowcount and multiple assignment</title><link>http://www.sqlservercentral.com/Forums/Topic902860-1711-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/69462/"&gt;Rowcount and multiple assignment&lt;/A&gt;[/B]</description><pubDate>Tue, 13 Apr 2010 21:22:19 GMT</pubDate><dc:creator>honza.mf</dc:creator></item></channel></rss>