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
 Transact-SQL (2000)
 Joining table

Author  Topic 

vije
Starting Member

2 Posts

Posted - 2005-09-23 : 04:33:44
Hi,
I've a problem joining 2 table. Say the tables are T1 and T2. I need SELECT T2.* from T2 WHERE T2.code = "@code" then I want ot join this results with T1 where T1.ID = T2.ID and need to show all the recods of T1. Any help appriciated.
Cheers.
-VJ

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-23 : 04:45:14
Try this

Select T1.* from Table1 T1 inner join Table2 T2 on T1.Id=T2.Id where T2.Code=@Code

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

vije
Starting Member

2 Posts

Posted - 2005-09-23 : 04:54:41
hi Madhivanan,
Thanks for the reply but the where clause filters the records to meet the T2.@code = @Code criteria. I need to show all the records of T1.
-VJ
Go to Top of Page

raclede
Posting Yak Master

180 Posts

Posted - 2005-09-23 : 04:58:35
<code>
SELECT T1.*
FROM T1
LEFT JOIN T2 ON (T1.ID=T2.ID) AND T2.Code = @Code

</code>
Go to Top of Page

Sharky
Starting Member

23 Posts

Posted - 2005-09-23 : 05:12:07
When you say all the records from T1, do you mean even those that don't match an ID in T2? Then you would have to do a LEFT OUTER JOIN thus:

SELECT
T1.*
FROM
dbo.T1 AS a
LEFT OUTER JOIN
(
SELECT T2.[ID]
FROM dbo.T2
WHERE T2.Code = @Code
) AS b ON (b.[ID] = T1.[ID]

bearing in mind that you will have NULLs for those T2.IDs that don't have a T1.ID match.
Go to Top of Page
   

- Advertisement -