Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Union vs. Join

Author  Topic 

Liat001
Starting Member

17 Posts

Posted - 2010-06-06 : 08:57:43
Hi all,
What is the better preformance to use?
Union or join,
I need to get data from several tables, sometimes from all of them and sometimes not.

What do you recommand?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-06 : 10:15:41
You can't compare union with join.
Give us an example for what you want to do.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-06-06 : 10:59:29
UNION adds data (more records, same number of columns) to your existing resultset.
JOIN adds more columns (and in some cases, more records) to your existing resultset.

UNION is vertical, JOIN is horizontal.

A special case is CROSS JOIN which gives you both more records and more columns.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Liat001
Starting Member

17 Posts

Posted - 2010-06-07 : 02:33:10
select
when case (charindex ('x',@A)>0)then a.AName
else when case(charindex ('y',@A)>0) then b.Bname
else ...

from c
join left b on c.id=b.id
join left a on c.id=a.id
...

OR

--I will create an empty table with the same columns befor

union
select a.name As name
from a
join left c on c.id=a.id
where (charindex ('x',@A)>0)

union
select b.name As name
from b
join left c on c.id=b.id
where (charindex ('y',@A)>0)

...




Go to Top of Page
   

- Advertisement -