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 |
sonia_newbie
Starting Member
17 Posts |
Posted - 2013-03-07 : 00:59:20
|
hII have 2 tables names named parent and child.parent:name P_id detailsdata0878 45 jilliandata0881 85 lugodata0867 83 cardozachildname P_id modeldata0878 45 +1data0878 45 -1data0878 45 +1data0878 45 -1data0867 83 -1data0867 83 +1data0867 83 +1data0867 83 -1data0867 83 +1data0867 83 -1i want to update the table child by looking up table parent (basically I want to update P_id of child by replacing it with P_id of parent comparing the name (as a parameter in both tables.)Is this command correct?UPDATE child FROM parent P_id= 45where name = data0878I amnot sure whether this is correctwhat should i try?In the example i have pasted in table child , when name = data0878 the pid is same as in the parent but i want to check whether the P_id for a particular name remains the same as parent.am soo confused.. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-07 : 01:02:50
|
[code]UPDATE cSET c.P_id = p.P_IdFROM Child cJOIN Parent pON p.[name] = c.[name]WHERE p.P_id <> c.P_id[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2013-03-07 : 01:04:09
|
[code]UPDATE cSET P_id = p.P_idFROM child as cJOIN parent as pOn p.name = c.name[/code]Have a look at these:http://msdn.microsoft.com/en-us/library/ms190014(v=sql.105).aspxhttp://www.w3schools.com/sql/sql_join_inner.asphttp://msdn.microsoft.com/en-us/library/ms177523.aspx |
|
|
ahmeds08
Aged Yak Warrior
737 Posts |
Posted - 2013-03-07 : 01:09:12
|
Is this you are looking for:update child set p_id=p.p_id from parent pinner join child con c.p_id=p.p_idwhere c.name=p.name |
|
|
sonia_newbie
Starting Member
17 Posts |
Posted - 2013-03-10 : 00:46:02
|
thanks all !! it worked!! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-10 : 04:08:34
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|