Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
i want to union all two queries with second query having order by clause.
examp:

select id,name from table 1
union all
select id,name from table 2 order by name.

in my expected result order the second query result only,not the entire result.
Posted
Comments
Prakash Thirumoorthy 22-Mar-13 1:45am    
have u used temporary table?
josh-jw 22-Mar-13 1:51am    
no.

Hi
Try This....
SQL
select id,name from table_1
union all
SELECT T.ID,T.name 
FROM (select TOP 100 PERCENT id,name from table_2 order by name) T


Check the following link also...
Using Derived Tables to Simplify the SQL Server Query Process[^]
Regards,
GVPrabu
 
Share this answer
 
v4
Comments
Maciej Los 22-Mar-13 2:50am    
Good job, my 5!

Thanks for your comment ;)
gvprabu 22-Mar-13 3:13am    
Thanks for ur Concern... :-)
josh-jw 22-Mar-13 2:54am    
ERROR SHOWING
"The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified"
gvprabu 22-Mar-13 3:00am    
check now.. u can add TOP
josh-jw 22-Mar-13 3:11am    
How we can predict that only 100 records will be there?if it contains more than 100 , what we will do?
hi,

use the following query



SQL
create table #temp (ID int, name varchar(50))

Insert into #temp   select id,name  from table1
Insert into #temp   select id,name  from table2 order by ID desc

select * from #temp
drop table #temp



it may help you..

Regards,
Prakash.T
 
Share this answer
 
v2
Comments
josh-jw 22-Mar-13 2:12am    
thanks for reply.but i would like a query without using a temperory table.is there any other way to do this functionality?
Prakash Thirumoorthy 22-Mar-13 2:22am    
I think, may be there is no option for that...
gvprabu 22-Mar-13 2:42am    
we have option... use Derived tables like as my Solution :-)
Prakash Thirumoorthy 22-Mar-13 2:51am    
nice :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900