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
 SQL Server Development (2000)
 Master detail Joins

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-04-30 : 09:29:26
Sanjay writes "Gurus,
I am a regular visitor to your site and find this site very useful.

I have a scenerio where I have a table Members which is master table and then we have Addresses table which can contain multiple records for a member. Similarly we have another Child table Emails which can also contain multiple email addresses.

Both addresses and Emails are related to Members but do not have relationship directly.

I want to select all rows from three tables for MemberID = 1.

If I use
SELECT Members.MemberId , Emails.EmailID , Addresses.AddressID from Members , Emails , Addresses where Members.MemberId = Addresses.AddressId And Members.MemberId = Emails.MemberID AND Memebers.MemberID = 1

If my table contains 3 addresses and 4 emails for memberID = 1
I will get 12 records

My question is can I join the table in another way just to get 4 records and in the 4th record get AddressId = Null as we get in outer Joins.

Thanks a lot

Sanjay"

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2002-04-30 : 09:36:43
Not sure what you're trying to do - can you show the basic designs of the tables and an example of what you want to show?

Thanks

Go to Top of Page

MuffinMan
Posting Yak Master

107 Posts

Posted - 2002-04-30 : 10:16:14
You can pretty much do anything you can think of in SQL:


set nocount on

-- ***************************************************
-- ** START: Set up tables and fill with test data
-- ***************************************************
create table #Members (
MemberID int identity(1,1) not null primary key clustered
, Member_Name varchar(20) null
)

create table #Addresses (
AddressID int identity(1,1) not null primary key clustered
, MemberID int not null
, Address varchar(100) not null
)

create table #Emails (
EmailID int identity(1,1) not null primary key clustered
, MemberID int not null
, Email varchar(100) not null
)

insert #Members (Member_Name) Values ('Joe User')

insert #Addresses (MemberID , Address) Values ( 1 , '123 Main Street')
insert #Addresses (MemberID , Address) Values ( 1 , '333 Broadway Road')
insert #Addresses (MemberID , Address) Values ( 1 , '9332 Centre Street')

insert #Emails (MemberID , Email) Values ( 1 , 'joe@eee.com')
insert #Emails (MemberID , Email) Values ( 1 , 'joe@3ewseee.com')
insert #Emails (MemberID , Email) Values ( 1 , 'juser@ppoppo.com')
insert #Emails (MemberID , Email) Values ( 1 , 'joe.user@abbdn.com')
-- ***************************************************
-- ** END : Set up tables and fill with test data
-- ***************************************************
go

-- ***************************************************
-- ** START: Create SP to return data
-- ***************************************************
create procedure #GetMemberData
@MemberID int
AS

set nocount on

create table #tempEmails (
RowId int identity(1,1) not null primary key clustered
, EmailID int not null
)

create table #tempAddresses (
RowId int identity(1,1) not null primary key clustered
, AddressID int not null
)

Insert #tempEmails (EmailID)
Select EmailID from #Emails where MemberID = @MemberID Order by EmailID

Insert #tempAddresses (AddressID)
Select AddressID from #Addresses where MemberID = @MemberID Order by AddressID

select @MemberID As MemberID , e.EmailID , a.AddressID
from #tempEmails e
full outer join #tempAddresses a
on e.RowID = a.RowID

drop table #tempEmails, #tempAddresses

return 0
go
-- ***************************************************
-- ** END : Create SP to return data
-- ***************************************************


-- Call the SP and get the data
exec #GetMemberData @MemberID = 1

-- drop temp tables and procs
drop table #Members , #Addresses , #Emails
drop procedure #GetMemberData

-- here is the output:
MemberID EmailID AddressID
----------- ----------- -----------
1 1 1
1 2 2
1 3 3
1 4 NULL



Go to Top of Page

sanjay
Starting Member

3 Posts

Posted - 2002-05-05 : 12:26:53
MuffinMan,

I got the direction and customised it as per my need.

Thanks a lot,
Sanjay
Go to Top of Page
   

- Advertisement -