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 |
|
anjali66
Starting Member
23 Posts |
Posted - 2011-01-12 : 13:03:40
|
| I have two tables master and comp table. In master table, I have columns called LB1, LB2, LB1_ID, LB2_ID and in my comp table I have ID and CompName. I need to update my master table the following wayIf LB1 in master table has a value of 'XYZ' then I need to find that value in Comp Table in column compName and then put the ID from comp table to master table in LB1masterLB1 LB2 LB1_ID LB2_IDXYZ HIJABC KLMDEF RAWPQR VQSCompID CompName 1 XYZ 2 ABC 3 RAW 4 DEF 5 VQS 6 HIJ 7 KLM 8 PQRI need to poulate my master table like thismasterLB1 LB2 LB1_ID LB2_IDXYZ HIJ 1 6ABC KLM 2 7DEF RAW 4 3PQR VQS 8 5 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-12 : 13:09:05
|
| [code]select m.*,c1.ID,c2.IDfrom master minner join comp c1on c1.CompName = m.LB1inner join comp c2on c2.CompName = m.LB2[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
anjali66
Starting Member
23 Posts |
Posted - 2011-01-12 : 13:15:14
|
| I need to do the update of the master table. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-12 : 13:16:55
|
just change it to an updateupdate mset m.LB1_ID =c1.ID,m.LB2_ID = c2.IDFROM master minner join comp c1on c1.CompName = m.LB1inner join comp c2on c2.CompName = m.LB2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
anjali66
Starting Member
23 Posts |
Posted - 2011-01-12 : 13:31:47
|
| Thanks Visakh. |
 |
|
|
anjali66
Starting Member
23 Posts |
Posted - 2011-01-12 : 14:03:15
|
| After executing the update query in my master table, I still see NULL records in LB1_ID , LB2_ID, although there is value in LB1, LB2.I am not sure why this is happening.Any suggestions? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-13 : 11:16:16
|
may be only one of them have data not both. try likeupdate mset m.LB1_ID =COALESCE(c1.ID,m.LB1_ID),m.LB2_ID = COALESCE(c2.ID,m.LB2_ID)FROM master mleft join comp c1on c1.CompName = m.LB1left join comp c2on c2.CompName = m.LB2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|