Solved

compare the results ot two tables.

Posted on 2011-09-06
5
230 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

821 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