SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

aswindba1
Yak Posting Veteran

USA
61 Posts

Posted - 07/09/2013 :  14:26:38  Show Profile  Reply with Quote


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 tableB




TableA

Server ||||| AppName

ServerA ||||| App1
ServerB ||||| App2
ServerC ||||| App3
ServerD ||||| App4
ServerE ||||| App5

TableB


Server ||||| AppName

ServerA ||||| App1
ServerA ||||| App11
ServerA ||||| App111
ServerB ||||| App2
ServerB ||||| App22
ServerC ||||| App3
ServerC ||||| App33
ServerD ||||| App4
ServerD ||||| App44
ServerD ||||| App444
ServerE ||||| App5
ServerE ||||| App55
ServerE ||||| App555


Required Output in Table A


Server ||||| AppName

ServerA ||||| App1
ServerA ||||| App11
ServerA ||||| App111
ServerB ||||| App2
ServerB ||||| App22
ServerC ||||| App3
ServerC ||||| App33
ServerD ||||| App4
ServerD ||||| App44
ServerD ||||| App444
ServerE ||||| App5
ServerE ||||| App55
ServerE ||||| App555

Please help me.

Thanks

James K
Flowing Fount of Yak Knowledge

3322 Posts

Posted - 07/09/2013 :  14:30:43  Show Profile  Reply with Quote
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, AppName
FROM
	TableB b
WHERE NOT EXISTS
(SELECT * FROM TableA a WHERE
a.SERVER = b.SERVER AND a.Appname = b.Appname);
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 07/09/2013 :  14:32:40  Show Profile  Reply with Quote

INSERT INTO TableA (Server,AppName)
SELECT Server,AppName
FROM TableB b
WHERE NOT EXISTS (SELECT 1
FROM Table A
WHERE Server = b.Server
AND AppName = b.AppName
)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

aswindba1
Yak Posting Veteran

USA
61 Posts

Posted - 07/09/2013 :  15:33:56  Show Profile  Reply with Quote
Thank you folks..I appreicate your help.

That query absolutely worked for me.

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29908 Posts

Posted - 07/10/2013 :  03:27:59  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Use MERGE
MERGE	dbo.TableA AS tgt
USING	dbo.TableB AS src ON src.[Server] = tgt.[Server]
		AND src.AppName = tgt.AppName
WHEN	NOT MATCHED BY TARGET
		THEN	INSERT	(
					[Server],
					AppName
				)
			VALUES	(
					src.[Server],
					src.AppName
				);



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000