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 |
aswindba1
Yak Posting Veteran
62 Posts |
Posted - 2013-07-09 : 14:26:38
|
I hae to update the tableA. Server names are same in both tables but in tableB we have more Appnames for the same severs. So, I have to pull those AppNames with server names. Here I am showing the samll example.We need to get the missing appnames from tableBTableAServer ||||| AppNameServerA ||||| App1ServerB ||||| App2ServerC ||||| App3ServerD ||||| App4ServerE ||||| App5TableBServer ||||| AppNameServerA ||||| App1ServerA ||||| App11ServerA ||||| App111ServerB ||||| App2ServerB ||||| App22ServerC ||||| App3ServerC ||||| App33ServerD ||||| App4ServerD ||||| App44ServerD ||||| App444ServerE ||||| App5ServerE ||||| App55ServerE ||||| App555Required Output in Table AServer ||||| AppNameServerA ||||| App1ServerA ||||| App11ServerA ||||| App111ServerB ||||| App2ServerB ||||| App22ServerC ||||| App3ServerC ||||| App33ServerD ||||| App4ServerD ||||| App44ServerD ||||| App444ServerE ||||| App5ServerE ||||| App55ServerE ||||| App555Please help me.Thanks |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-07-09 : 14:30:43
|
Did you want to insert the missing rows into TableA, or just write a query to retrieve the data using both tables? If you want to insert the missing data, use this:INSERT INTO TableA SELECT Server, AppNameFROM TableB bWHERE NOT EXISTS(SELECT * FROM TableA a WHEREa.SERVER = b.SERVER AND a.Appname = b.Appname); |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-09 : 14:32:40
|
[code]INSERT INTO TableA (Server,AppName)SELECT Server,AppNameFROM TableB bWHERE NOT EXISTS (SELECT 1FROM Table AWHERE Server = b.ServerAND AppName = b.AppName)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
aswindba1
Yak Posting Veteran
62 Posts |
Posted - 2013-07-09 : 15:33:56
|
Thank you folks..I appreicate your help.That query absolutely worked for me. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-07-10 : 03:27:59
|
Use MERGEMERGE dbo.TableA AS tgtUSING dbo.TableB AS src ON src.[Server] = tgt.[Server] AND src.AppName = tgt.AppNameWHEN NOT MATCHED BY TARGET THEN INSERT ( [Server], AppName ) VALUES ( src.[Server], src.AppName ); N 56°04'39.26"E 12°55'05.63" |
 |
|
|
|
|