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
 Mutiple tables and joins

Author  Topic 

tech_1
Posting Yak Master

129 Posts

Posted - 2011-12-10 : 19:21:55
Hi.

I have 3 tables:

TableA:
PageID
SubPageID

TableB:
PageID
Title

TableC:
SubPageID
PageID
Title


what I want to do is to be able to join them together (for a SPROC) and using the parameters to match, with "@subPageID" being a possible NULL value.

I want to return the results of Either:

TableB

OR

TableB and TableC (together) in one resultset


how can I do this?

TableA is indeed joined as an FK to TableB and TableC

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-11 : 00:56:08
[code]
CREATE PROC yourProcName
@subPageID int = NULL
AS
SELECT *
FROM TableA a
INNER JOIN TableB b
ON b.PageID = a.PageID
INNER JOIN TableC c
ON c.SubPageID = a.SubPageID
AND c.PageID = a.PageID
WHERE (c.SubPageID =@subPageID
OR @subPageID IS NULL)
GO
[/code]

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

Go to Top of Page
   

- Advertisement -