Solved

comparing two tables in different servers in sql server 2005

Posted on 2013-01-22
4
260 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

914 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

18 Experts available now in Live!

Get 1:1 Help Now