Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

compare the results ot two tables.

Posted on 2011-09-06
5
Medium Priority
?
237 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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

885 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