Solved

comparing two tables in different servers in sql server 2005

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

762 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

22 Experts available now in Live!

Get 1:1 Help Now