Solved

compare the results ot two tables.

Posted on 2011-09-06
5
227 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi, The database resides in different server also.
0
 
LVL 8

Accepted Solution

by:
Ghunaima earned 500 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now