Solved

compare the results ot two tables.

Posted on 2011-09-06
5
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

734 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