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.
| Author |
Topic |
|
gbastos
Starting Member
1 Post |
Posted - 2008-07-15 : 09:40:53
|
| Hi,I have two tables within a SQL Server database; the first database has a column which is an identifier and another column which lists one of more email identifers for a second table, e.g.ID EmailID-- ----------1 1002 101,102,103,1043 105,106The second table has a column which has an email identifier and another column which lists one email address for that particular identifier, e.g.EmailID EmailAddress--------- ------------------100 address100@email.com101 address101@email.com102 address102@email.com103 address103@email.com104 address104@email.com105 address105@email.com106 address106@email.comI need to create a stored procedure or function that:1. Selects an EmailID from the first table, based on a valid ID, 2. ?Splits? the EmailID field of the first table (using the comma separator) so that there is an array of EmailIDs and then,3. Selects the relevant EmailAddress value from the second table, based on a valid EmailID stored in the arrayIs there any way that this can be done directly within SQL Server using a stored procedure/function, without having to select and separate the EmailIDs using a separate development language and then making repeated calls to the database in order to select the relevant EmailAddress? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-07-15 : 09:50:35
|
You shoud try normalise the tableTry thisSelect t1.Emailid, t2.EmailAddress from table1 t1 inner join table2 t2 on ','+t1.Emailid+',' like '%,'+cast(t2.Emailid as varchar(5))+',%' MadhivananFailing to plan is Planning to fail |
 |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2008-07-15 : 20:06:22
|
| gbastos,Your schema seems turned inside out. It sounds like your first table holds some entity (people) that can have a set of emails associated. Your second table holds the email details.Have you considered changing the schema so that all of the email information lies within the email table.i.e.create table entity ( id int, ... other entity information)create table Emails ( ParentID int, EmailAddress varchar(256))Now you can join the child to the parent using the parent's keyselect p.id, p.entityname, c.EmailAddressfrom entity p inner join emails c on p.id = c.ParentIDPut a foreign key on the child table so that you don't get dangling references and you're good to go...=======================================Some people walk in the rain, others just get wet. -Roger Miller, musician (1936-1992) |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
|
|
|
|
|