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
 Join advice

Author  Topic 

1821
Starting Member

18 Posts

Posted - 2011-12-27 : 06:47:34
Hi guys,

I have a table with two columns I am selecting from and some of the data in the second column has a reference that I need to use in order to gain data from another column in a different table.

The issue is that when I do the join it limits the fields returned but I would like to return everything. Can this be achieved with a different type of join?

Thanks

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-12-27 : 06:51:35
Can you post the query that you are using? Also, post the table DDL if you can. It may be that you need a LEFT join instead of a simple (INNER) JOIN. But, hard to tell without seeing your current query.

If you need help in getting the DDL etc. and a little bit of guidance on how to post, look up Brett's blog here: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-27 : 06:51:56
yep...it can be achieved by using left join instead of inner join. it will return all from master table(left had side) and matches in case it has one from right side and otherwise it returns NULL values

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-27 : 06:53:42
Also one thing to note here is that in case you've some filters (WHERE condition) based on your reference table columns then you need to move them to ON condition instead of WHERE condition as explained below

http://weblogs.sqlteam.com/jeffs/archive/2007/05/14/criteria-on-outer-joined-tables.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

1821
Starting Member

18 Posts

Posted - 2011-12-27 : 07:25:25
Hi guys,

Thanks for the advice - the left join is exactly what I needed!

Thanks again!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-27 : 07:33:42
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -