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

x
?
Solved

comparing two tables in different servers in sql server 2005

Posted on 2013-01-22
4
Medium Priority
?
269 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 400 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 60

Accepted Solution

by:
Kevin Cross earned 600 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

971 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