Click here to Skip to main content
15,914,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, good morning,

I have problem with stored procedure.

In SQL stored procedure I make 3 temp table like #table1,#table2,#table3 and all three table have 2 column like #table1(total1,Code),#table2(total2,Code) ,#table3(total3,Code).
In these 3 table code is data is same but total field have different value,#table1 and #table2 have data every time but #table3 have almost empty record but some case #table3 also have record.

My select Query is given below.

SQL
select t1.total1,t2.total2,t3.total3
from #table1 t1 
inner join #table2 t2 on t1.Code=t2.Code
inner join #table3 t3 on t2.code=t3.Code  

It's not displaying the t1.total1,t2.total2,t3.total3 value if #table3 is empty record but if #table3 not empty then its show the value.

So please help me how I solve this issue

thanks


[edit]Capitalization, code block, general tidy - OriginalGriff[/edit]
[edit]Oops: spelling error in my modification notes... - OriginalGriff[/edit]
Posted
Updated 9-Mar-11 20:21pm
v4

You can try using outer joins instead of inner joins.
This will help you get data even when table 3 is empty.
 
Share this answer
 
Use following Query (this tested with dummy database on my end)

SQL
SELECT t1.total1,t2.total2,t3.total3 FROM #table1 t1
INNER JOIN  #table2 t2 ON t1.code = t2.code
LEFT OUTER JOIN  #table3 t3 ON t1.code = t3.code


Thanks,
Imdadhusen
 
Share this answer
 
Case 1 - #table1 contains all codes
SQL
select t1.code,t1.total1,t2.total2,t3.total3
from #table1 t1
left join #table2 t2 on t2.Code=t1.Code
left join #table3 t3 on t3.code=t1.Code


Case 2 - none of the tables contains all codes
SQL
select c.code,t1.total1,t2.total2,t3.total3
from   (
       -- all codes
       select code from #table1
       union
       select code from #table2
       union
       select code from #table3
       ) c
left join #table1 t2 on t1.Code=c.Code
left join #table2 t2 on t2.Code=c.Code
left join #table3 t3 on t3.code=c.Code
 
Share this answer
 
v2
Try making the second join a RIGHT OUTER JOIN: See http://en.wikipedia.org/wiki/Join_(SQL)[^] - from how I read your question, that may solve the problem.
 
Share this answer
 

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