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 2008 Forums
 Transact-SQL (2008)
 Update query help

Author  Topic 

littlewing
Starting Member

33 Posts

Posted - 2012-04-03 : 08:47:44
I have this (awful) schema where the foreign keys are stored as a comma separated list in a column (FMID in table 2 below). Table 1 is a table with these keys in the second column (FMID). I need to add a new column to table 2, call it NEWFMID and in it store the PK from table 1 as shown. The problem is how to join for each FMID in the comma separated list in table 2 and save the new keys as a comma separated list in NEWFMID as well. Normalizing the db is not an option unfortunately, it would require a lot of change to 10 year old ASP application code. FMID can be null or empty in table 2 and null in table 1.



Table 1 (int, nvarchar(100))

ID FMID
1 L1005
2 L1008
3 L3185
4 L1013
5 L1018
6 NULL
7 L1028
8 L1038
9 L1041
10 NULL


table 2 (int, varchar(100))

PID FMID
85 NULL
283 L1005
285 L1008
287 L1005
292 L1018
300 L1018
372 L1005,L1008
374 L1041
381
382
384 L1008,L1028
387 L1008
390 L1005,L1008,L1018,L1041

table 2 with new column (int varchar(100), varchar(100))

PID FMID NEWFMID
85 NULL NULL
283 L1005 1
285 L1008 2
287 L1005 1
292 L1018 5
300 L1018 5
372 L1005,L1008 1,2
374 L1041 9
381
382
384 L1008,L1028 2,7
387 L1008 2
390 L1005,L1008,L1018,L1041 1,2,5,9




Thanks for any help you can provide.

LW

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-04-03 : 09:39:50
1) Hunt down the person who designed this db.
2) Harm them.
3) Profit.









How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-04-03 : 09:42:19
On the serious side, you may be able to create a UDF to parse the list, select what you need, then insert it after. I wish you lots of luck.









How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

littlewing
Starting Member

33 Posts

Posted - 2012-04-03 : 15:00:41
Ok to make this simpler how can I turn this:


PID FMID
85 NULL
283 L1005
285 L1008
287 L1005
292 L1018
300 L1018
372 L1005,L1008
374 L1041
381
382
384 L1008,L1028
387 L1008
390 L1005,L1008,L1018,L1041

into this in a new table


NPID NFMID

283 L1005
285 L1008
287 L1005
292 L1018
300 L1018
372 L1005
372 L1008
374 L1041
384 L1008
384 L1028
387 L1008
390 L1005
390 L1008
390 L1018
390 L1041



Essentially split the comma separated list into a new table with it's associated PID.

Thank you for any help.

LW
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-04-03 : 15:11:15
Didn't we do this with Cross Apply just recently?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

littlewing
Starting Member

33 Posts

Posted - 2012-04-03 : 15:44:46
Yes CROSS APPLY and a Split function I had kicking around have done the trick.

Thanks.
Go to Top of Page
   

- Advertisement -