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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query help

Author  Topic 

aswindba1
Yak Posting Veteran

62 Posts

Posted - 2013-02-05 : 14:43:15
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
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-05 : 14:53:11
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
Go to Top of Page
   

- Advertisement -