Solved

comparing two tables in different servers in sql server 2005

Posted on 2013-01-22
4
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 60

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

630 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