Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Rowcount and multiple assignment Expand / Collapse
Author
Message
Posted Tuesday, April 13, 2010 9:22 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, September 22, 2014 1:20 AM
Points: 1,387, Visits: 1,315
Comments posted to this topic are about the item Rowcount and multiple assignment



See, understand, learn, try, use efficient
© Dr.Plch
Post #902860
Posted Tuesday, April 13, 2010 9:50 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 11:58 AM
Points: 1,676, Visits: 1,758
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

Post #902864
Posted Wednesday, April 14, 2010 12:15 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, September 22, 2014 1:20 AM
Points: 1,387, Visits: 1,315
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
Post #902890
Posted Wednesday, April 14, 2010 2:01 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 4:59 PM
Points: 4,023, Visits: 5,324
Well thought out question, well done.

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Post #902917
Posted Wednesday, April 14, 2010 3:23 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 12:51 AM
Points: 3,067, Visits: 955
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.
Post #902950
Posted Wednesday, April 14, 2010 6:14 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 6:55 AM
Points: 880, Visits: 876
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
Post #903041
Posted Wednesday, April 14, 2010 6:42 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, September 22, 2014 1:20 AM
Points: 1,387, Visits: 1,315
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
Post #903072
Posted Wednesday, April 14, 2010 6:52 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 6:55 AM
Points: 880, Visits: 876
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
Post #903082
Posted Wednesday, April 14, 2010 7:50 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, February 4, 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
Post #903140
Posted Wednesday, April 14, 2010 8:05 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, September 22, 2014 1:20 AM
Points: 1,387, Visits: 1,315
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
Post #903159
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse