Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CROSS JOIN with one empty table sql server

I have two tables say MyFull and MyEmpty. I need to do the combination of all records from both tables. Sometimes, MyEmpty table might have no records, in this scenario I need to return all records from MyFull. This is what I have tried:

 --Q1 >> returns no results
 SELECT *
 FROM MyFull CROSS JOIN MyEmpty 

--Q2 >> returns no results 
SELECT *
FROM MyFull, MyEmpty

I though of using LEFT JOIN but I have no common key to join on. This is a SQLFiddle

like image 213
BICube Avatar asked Jun 19 '15 15:06

BICube


1 Answers

Full table :

declare @t table (Spot int,name varchar(1),pct int)
insert into @t(Spot,name,pct)values(1,'A',2),(1,'B',8),(1,'C',6),(2,'A',4),(2,'B',5),(3,'A',5),(3,'D',1),(3,'E',4)

Empty table :

declare @tt table (Spot int,name varchar(1),pct int)
select * from @t OUTER APPLY @tt 
like image 71
mohan111 Avatar answered Oct 18 '22 10:10

mohan111