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
62 Posts

Posted - 02/05/2013 :  14:43:15  Show Profile  Reply with Quote
I need to write a query by comparing the two tables and add the new data if any.

Table A and TableB

TableA has follwoing rows:
Server name
Application ID

TableB has follwoing rows:
Server name
Application ID

I need to compare TableA with TableB. We need to figure out what are the new serevrs are addedd in the tableB need to be inserted in TableA.

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 02/05/2013 :  14:53:11  Show Profile  Reply with Quote
TableA has follwoing rows:
I assume you meant columns?

There are a few different ways you can do this - here are couple of those:
INSERT INTO TableA
(ServerName, ApplicationID)
SELECT
	b.ServerName, b.ApplicationID
FROM
	TableB b
WHERE 
	NOT EXISTS 
	( SELECT * FROM TableA a WHERE
	a.ServerName = b.ServerName
	AND a.ApplicationId = b.ApplicationId
	);

INSERT INTO TableA
(ServerName, ApplicationID)
SELECT
	ServerName, ApplicationID
FROM
	TableB b
EXCEPT
SELECT
	ServerName, ApplicationID
FROM
	TableA

Edited by - James K on 02/05/2013 14:55:13
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