Solved

compare the results ot two tables.

Posted on 2011-09-06
5
229 Views
Last Modified: 2012-05-12
Hi,

I have database A and database B. Both has detailedCode table.

How can I compare the data of detailedCode table from database A to the data of detailedCode table from database B.

I need scripts that has

1) Script that will retrieve similar data.
2) Script that will retrieve data from detailedCode table from database A that is not in detailedCode table from database B.
3) Script that will retrieve data from detailedCode table from database B that is not in detailedCode table from database A.

0
Comment
Question by:PagodNaUtak
5 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 36493041
you are describing different kinds of joins. these things are best done on the database server or with temporary tables.

you want

1. SELECT * FROM A.detailedCode aa JOIN B.detailedCode bb WHERE aa.primarykey=bb.primarykey

2. SELECT * FROM A.detailedCode aa LEFT JOIN B.detailedCode bb WHERE bb.primarykey IS NULL

3. SELECT * FROM A.detailedCode aa RIGHT JOIN B.detailedCode bb WHERE aa.primarykey IS NULL

If you can't do it from within the DB, what scripting language are you using? Regardless, I would probably try to copy the table from one DB to the other (perhaps as a temp table) and then use one of the above queries.
0
 
LVL 39

Expert Comment

by:appari
ID: 36493099
correction in the queries

1. SELECT * FROM A.detailedCode aa JOIN B.detailedCode bb ON aa.primarykey=bb.primarykey

2. SELECT * FROM A.detailedCode aa LEFT JOIN B.detailedCode bb ON aa.primarykey=bb.primarykey
WHERE bb.primarykey IS NULL

3. SELECT * FROM A.detailedCode aa RIGHT JOIN B.detailedCode bb ON aa.primarykey=bb.primarykey
WHERE aa.primarykey IS NULL

0
 
LVL 8

Author Comment

by:PagodNaUtak
ID: 36493376
Hi, The database resides in different server also.
0
 
LVL 8

Accepted Solution

by:
Ghunaima earned 500 total points
ID: 36493475
First you have to create a linked server, then use

    <servername/IP>.<databasename>.<schema>.<tablename>

format for writing a tablename. e.g [192.168.0.19].database1.dbo.tablea or in case of named servers servr1.db1.dbo.tableb etc. For creating Linked server follow these steps
1:  Open management studio.
2:  Connect one server (database engine)
3:  Click on Server Objects to expand
4:  Right Click on Linked Server & Select "New Linked Server ..." & provide info Screen Shot
0
 
LVL 9

Expert Comment

by:mimran18
ID: 36493493
Hi,
   if the database resides in different server you need to create a linked server and modify the query  a little bit.
Like

1. SELECT * FROM A.detailedCode aa JOIN [LinkedserverName].[DatabaseName].dbo.B.detailedCode bb ON aa.primarykey=bb.primarykey

2. SELECT * FROM A.detailedCode aa LEFT JOIN [LinkedserverName].[DatabaseName].dbo.B.detailedCode bb ON aa.primarykey=bb.primarykey
WHERE bb.primarykey IS NULL

3. SELECT * FROM A.detailedCode aa RIGHT JOIN [LinkedserverName].[DatabaseName].dbo.B.detailedCode bb ON aa.primarykey=bb.primarykey
WHERE aa.primarykey IS NULL
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question