Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
Article Discussions
»
Article Discussions by Author
»
Discuss content posted by Carlo Romagnano
»
"SELECT @local_variable"
35 posts, Page 1 of 4
1
2
3
4
»
»»
"SELECT @local_variable"
Rate Topic
Display Mode
Topic Options
Author
Message
Carlo Romagnano
Carlo Romagnano
Posted Thursday, June 24, 2010 8:26 PM
SSCommitted
Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 1,996,
Visits: 1,864
Comments posted to this topic are about the item
"SELECT @local_variable"
Post #942789
malleswarareddy_m
malleswarareddy_m
Posted Thursday, June 24, 2010 9:42 PM
SSCommitted
Group: General Forum Members
Last Login: Today @ 7:53 AM
Points: 1,868,
Visits: 1,044
good question. but i also expected that result is A,B,C.
Malleswarareddy
I.T.Analyst
MCITP(70-451)
Post #942797
Danny Ocean
Danny Ocean
Posted Thursday, June 24, 2010 10:56 PM
Ten Centuries
Group: General Forum Members
Last Login: Today @ 6:52 AM
Points: 1,111,
Visits: 1,233
Really a good question with good explanation.
Keep Learning - Keep Growing !!!
http://growwithsql.blogspot.in
Thanks
Vinay Kumar
Post #942806
UMG Developer
UMG Developer
Posted Thursday, June 24, 2010 11:28 PM
SSCrazy
Group: General Forum Members
Last Login: Today @ 9:45 AM
Points: 2,163,
Visits: 2,151
Great question, and a very understandable explanation. Thanks!
Post #942808
rals
rals
Posted Friday, June 25, 2010 12:56 AM
SSCommitted
Group: General Forum Members
Last Login: Thursday, June 13, 2013 8:02 AM
Points: 1,732,
Visits: 757
Good Question.
SELECT @t = @t + @comma + a.a
,@comma = ','
FROM #a a
ORDER BY 1
Result : C
SELECT @t = @t + @comma + a.a
,@comma = ','
FROM #a a
ORDER BY a.a
Result : A,A,B,B,C,C
changing the order by giving different result...experts please explain?
Regards,
Rals
.
Post #942830
ziangij
ziangij
Posted Friday, June 25, 2010 1:08 AM
SSCommitted
Group: General Forum Members
Last Login: Friday, May 17, 2013 2:53 AM
Points: 1,552,
Visits: 359
since there is only one column, why is this working as well ?
SELECT @t = @t + @comma + a.a
,@comma = ','
FROM #a a
ORDER BY 2
Result : A,A,B,B,C,C
rajesh.subramanian (6/25/2010)
Good Question.
SELECT @t = @t + @comma + a.a
,@comma = ','
FROM #a a
ORDER BY 1
Result : C
SELECT @t = @t + @comma + a.a
,@comma = ','
FROM #a a
ORDER BY a.a
Result : A,A,B,B,C,C
changing the order by giving different result...experts please explain?
Post #942838
Christian Buettner-167247
Christian Buettner-167247
Posted Friday, June 25, 2010 1:29 AM
SSCrazy
Group: General Forum Members
Last Login: Today @ 12:36 AM
Points: 2,547,
Visits: 3,648
rajesh.subramanian (6/25/2010)
Good Question.
SELECT @t = @t + @comma + a.a
,@comma = ','
FROM #a a
ORDER BY 1
Result : C
SELECT @t = @t + @comma + a.a
,@comma = ','
FROM #a a
ORDER BY a.a
Result : A,A,B,B,C,C
changing the order by giving different result...experts please explain?
Well, in the first case (order by a.a) you are sorting the intermediate result set of selecting from #a.
In the second case (order by 1) you are sorting the variable itself. While the first case makes perfectly sense, the second one does not (how do you sort a variable?)
Best Regards,
Chris Büttner
Post #942846
Christian Buettner-167247
Christian Buettner-167247
Posted Friday, June 25, 2010 1:49 AM
SSCrazy
Group: General Forum Members
Last Login: Today @ 12:36 AM
Points: 2,547,
Visits: 3,648
ziangij (6/25/2010)
since there is only one column, why is this working as well ?
SELECT @t = @t + @comma + a.a
,@comma = ','
FROM #a a
ORDER BY 2
Result : A,A,B,B,C,C
There are two "columns", the second column is "@comma".
SQL Server "optimizes" this trivial ORDER BY by removing it completely. (If you think about it, you are ordering by a static value in this case which is the same for all rows). You can see this in the execution plan (there is no order by in the execution plan)
Now try the following instead, and you will see the sorting is included in the plan:
ORDER BY @comma + REPLACE(a,a,'')
Best Regards,
Chris Büttner
Post #942859
Cadavre
Cadavre
Posted Friday, June 25, 2010 2:15 AM
SSCrazy
Group: General Forum Members
Last Login: Today @ 2:53 PM
Points: 2,242,
Visits: 6,553
Damn, confused me, went with A,B,C
Not a DBA, just trying to learn
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...
(*)
Missing rows with nolock
(*)
Allocation order scans with nolock
(*)
Consistency issues with nolock
(*)
Transient Corruption Errors in SQL Server error log caused by nolock
(*)
Dirty reads, read errors, reading rows twice and missing rows with nolock
LinkedIn
| Blog coming soon (for sufficiently large values of "soon"
)!
Post #942877
ziangij
ziangij
Posted Friday, June 25, 2010 2:36 AM
SSCommitted
Group: General Forum Members
Last Login: Friday, May 17, 2013 2:53 AM
Points: 1,552,
Visits: 359
thanks, understood now...
Christian Buettner-167247 (6/25/2010)
ziangij (6/25/2010)
since there is only one column, why is this working as well ?
SELECT @t = @t + @comma + a.a
,@comma = ','
FROM #a a
ORDER BY 2
Result : A,A,B,B,C,C
There are two "columns", the second column is "@comma".
SQL Server "optimizes" this trivial ORDER BY by removing it completely. (If you think about it, you are ordering by a static value in this case which is the same for all rows). You can see this in the execution plan (there is no order by in the execution plan)
Now try the following instead, and you will see the sorting is included in the plan:
ORDER BY @comma + REPLACE(a,a,'')
Post #942887
« Prev Topic
|
Next Topic »
35 posts, Page 1 of 4
1
2
3
4
»
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.