Solved

comparing two tables in different servers in sql server 2005

Posted on 2013-01-22
4
263 Views
Last Modified: 2013-01-23
Hi,

I have to compare below 2 tables and need to insert groupname's into Table_A from Table_B .That means Table_B have all the groupnames (296) and Table_A have few groupnames(250).The first 3 columns are identical in both the tables

SERVER A

TEST_DB

Table_A  (3 columns)

groupid(int)  groupname(varchar)  groupcode(varchar)

10                        group1                       g1                              


20                         group2                       g2              


SERVER B

SAMPLE_DB

Table_B (5 columns)

rsgroupid(int)  groupname(varchar)  groupcode(varchar)  updatedate(date)      updatedby  

10                        group1                            g1                           10/31/2012             xxx


20                        group2                            g2                        1/31/2012                  yyy


30                         group3                            g3                      10/31/2012                 zzzz


Thanks in Advance
0
Comment
Question by:ksd123
4 Comments
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 100 total points
ID: 38806643
insert into tablea
select rsgroupid, groupname, groupcode from tableb
except
select groupid, groupname, groupcode from tablea
0
 
LVL 1

Expert Comment

by:goldykhurmi
ID: 38806987
insert into servera.tablea
select rsgroupid, groupname, groupcode from serverb.tableb left join servera.tablea on
servera.tablea.groupid=serverb.tableb.rsgroupid where servera.tablea.groupid is null

Goldy Khurmi
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 150 total points
ID: 38807259
A combination approach should work. awking00's method using EXCEPT should get you the rows from tableb that are not in the results from tablea. This is the same as using NOT EXISTS.

e.g.
INSERT INTO tablea(groupid, groupname, groupcode)
SELECT rsgroupid, groupname, groupcode
FROM tableb
WHERE NOT EXISTS (
    SELECT 1
    FROM tablea
    WHERE tablea.groupid = tableb.rsgroupid
);

Open in new window


However, in your case, if the tables are on different servers, you will have to use four-part naming, which I believe is Goldy's suggestion despite only showing two parts, for the remote table. Therefore, you should connect to server/database with tablea and use tableb_server.tableb_database.tableb_schema.tableb.

For example: MyRemoteServer.RemoteDB.dbo.tableb

I hope that helps!

P.S. MSDN (adding linked server): http://msdn.microsoft.com/en-us/library/ms190479.aspx
0
 

Author Closing Comment

by:ksd123
ID: 38811044
Thank you.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS - How do I get a year value to display based on the current month? 8 42
SQL Help 27 45
MS SQL + Insert Into Table - If Doesnt Exist 9 35
SQL view 2 27
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

778 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