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
 General SQL Server Forums
 New to SQL Server Programming
 CROSS JOIN

Author  Topic 

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2013-12-05 : 09:38:53
Hi All,

I am stuck in a situation where I am trying to use CROSS JOIN.
But this not solving my problem at all.I am using example to
show my problem.


Declare @a table( a int)
INSERT into @a values (1)

Declare @b table( b int)
INSERT into @b VALUEs(2)

Declare @c table( c int)
INSERT into @c VALUEs(3)

SELECT t1.a,t2.b,t3.c from @a t1 CROSS JOIN
@b t2 CROSS JOIN @c t3

Above script gives me correct information like

a b c
1 2 3

But the Problem is if any of the table is blank it does
not return any thing for example


Declare @a table( a int)
INSERT into @a values (1)

Declare @b table( b int)
--INSERT into @b VALUEs(2)

Declare @c table( c int)
INSERT into @c VALUEs(3)

SELECT t1.a,t2.b,t3.c from @a t1 CROSS JOIN
@b t2 CROSS JOIN @c t3


Table @b is empty and this Select query gives
nothing.
How can I fix this even after any of the table is blank
I should get the column with NULL or blank value.





Vijay is here to learn something from you guys.

Ifor
Aged Yak Warrior

700 Posts

Posted - 2013-12-05 : 09:49:20
This is what CROSS JOIN is meant to do.

It is difficult to tell, but maybe you want a FULL JOIN:

SELECT t1.a,t2.b,t3.c
from @a t1
FULL JOIN @b t2 ON 1=1
FULL JOIN @c t3 ON 1=1
Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2013-12-05 : 09:56:45
Thanks Ifor.That's what I wanted to get

Vijay is here to learn something from you guys.
Go to Top of Page
   

- Advertisement -