July 13, 2013 at 3:25 pm
Comments posted to this topic are about the item TSQL-Order by
Pramod
SQL Server DBA | MCSE SQL Server 2012/2014
in.linkedin.com/in/pramodsingla/
http://pramodsingla.wordpress.com/
July 14, 2013 at 10:58 pm
easy one...!!! π
July 14, 2013 at 11:07 pm
Easy one for Monday morning π
Thanks Pramod!
~ Lokesh Vij
Link to my Blog Post --> www.SQLPathy.com[/url]
Follow me @Twitter
July 14, 2013 at 11:25 pm
Lokesh Vij (7/14/2013)
Easy one for Monday morning πThanks Pramod!
+1 π
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
July 14, 2013 at 11:32 pm
When I checked the BOL i found that there is a line stating:
If a table name is aliased in the FROM clause, only the alias name can be used to qualify its columns in the ORDER BY clause.
I did some modification in the query and try to run following scenarios and these all are working.
select a ss from test t
order by ss
select t.a ss from test t
order by a
select a from test t
order by a
Can anyone tell me what does that BOL line means?
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
July 14, 2013 at 11:46 pm
Thanks for start Monday with easy one π
Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
July 14, 2013 at 11:58 pm
kapil_kk (7/14/2013)
When I checked the BOL i found that there is a line stating:If a table name is aliased in the FROM clause, only the alias name can be used to qualify its columns in the ORDER BY clause.
I did some modification in the query and try to run following scenarios and these all are working.
select a ss from test t
order by ss
select t.a ss from test t
order by a
select a from test t
order by a
Can anyone tell me what does that BOL line means?
According to BOl, If you have given a alias name to column name then you can not use column name in order by clause.
the 2nd statement should give error like "invalid column name "a" ", but it doesn't. I am also looking for reason why 2nd T-sql code execute successfully.
Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
July 15, 2013 at 12:58 am
You haven't read it carefully. It says: "If a table name is aliased, only the alias name can be used to qualify its columns in the ORDER BY clause."
So this will not work:
select a from test t
order by test.a
_______________________________________________
www.sql-kefalo.net (SQL Server saveti, ideje, fazoni i fore)
July 15, 2013 at 1:14 am
Interesting question for a Monday π
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
July 15, 2013 at 1:48 am
Easy easy easy.
Nice start to the week.
July 15, 2013 at 1:50 am
Danny Ocean (7/14/2013)
kapil_kk (7/14/2013)
When I checked the BOL i found that there is a line stating:If a table name is aliased in the FROM clause, only the alias name can be used to qualify its columns in the ORDER BY clause.
I did some modification in the query and try to run following scenarios and these all are working.
select a ss from test t
order by ss
select t.a ss from test t
order by a
select a from test t
order by a
Can anyone tell me what does that BOL line means?
According to BOl, If you have given a alias name to column name then you can not use column name in order by clause.
the 2nd statement should give error like "invalid column name "a" ", but it doesn't. I am also looking for reason why 2nd T-sql code execute successfully.
Thanks i got it... π
Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
July 15, 2013 at 1:57 am
Thanks for the question
Best Regards,
Chris BΓΌttner
July 15, 2013 at 2:12 am
This was removed by the editor as SPAM
July 15, 2013 at 5:06 am
nenad-zivkovic (7/15/2013)
You haven't read it carefully. It says: "If a table name is aliased, only the alias name can be used to qualify its columns in the ORDER BY clause."So this will not work:
select a from test t
order by test.a
+1
But this applies for the whole query, not only to the ORDER BY clause. It's funny, though, that if you alias the column, both names can be used (original & alias) in the ORDER BY clause
Create table #test(a int)
insert into #test values (null)
insert into #test values (2)
insert into #test values (3)
insert into #test values (1)
insert into #test values (null)
-- Will work
select a as col1 from #test as t
order by col1 -- ORDER BY alias
select a as col1 from #test as t
order by a -- ORDER BY column name
-- Will NOT work
select #test.a as col1 from #test as t
order by col1
select a as col1 from #test as t
order by #test.a
drop table #test
Cheers
July 15, 2013 at 5:17 am
Thanks for your replies I got it now π
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Viewing 15 posts - 1 through 15 (of 24 total)
You must be logged in to reply to this topic. Login to reply