UNION ALL And Order BY


Dear Friends
 
                    I have come across feature in SQL server which is less known to developers. Union ALL and Order by together. Fill combo box with union of two tables and order by Ascending. I would normally write it as
 
SQL :-
Select  PersonName From
(
    Select PersonName From TranPersonDetails
    Union All
    Select CompanyName From TranCompanyDetails
) AllData
Order by PersonName
 
Look into above SQL :- Additonal Select statment on top to add order by. Found easier way to provide order without Top Select Statement
 
SQL :-
Select PersonName From TranPersonDetails
Union All
Select CompanyName From TranCompanyDetails
Order By PersonName
 
No need to have additonal Select Statment.Order by will work for both queries, 
This even works with SQL SERVER 2000. 

Comments

Popular posts from this blog

Shared / Static Class in vb.net/C#

Xamarin Forms : PopUp Page with RG.Plugins.Popup

Text was truncated or one or more characters had no match in the target code page.". (SQL Server Import and Export Wizard)