Sql server union but keep order

Afroman Makgalemela

Is there a way to union two tables, but keep the rows from the first table appearing first in the result set?

For example:

Table1

name        surname
-------------------
John         Doe
Bob          Marley
Ras          Tafari

Table2

name       surname
------------------
Lucky      Dube
Abby       Arnold

I want the result set to be:

name        surname
-------------------
John         Doe
Bob          Marley 
Ras          Tafari
Lucky      Dube
Abby       Arnold

Unfortunately, union somehow reorders the table. Is there a way around this?

praveen

Try this :-

Select * 
from
( 
Select name,surname, 1 as filter
from  Table1
Union all
Select name,surname , 2 as filter
from Table2
)
order by filter

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related