Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Compare two fields in two databases

Author  Topic 

shabar
Starting Member

4 Posts

Posted - 2010-09-09 : 07:12:12
Hi

How to compare two fields in two separate SQL server databases.


Cheers

Shabar

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2010-09-09 : 07:37:59
what comparison do you want to run ? sample data and output will help.
Go to Top of Page

shabar
Starting Member

4 Posts

Posted - 2010-09-09 : 15:10:39
quote:
Originally posted by sakets_2000

what comparison do you want to run ? sample data and output will help.



Hi

Basically I want to run one SQL script to compare two tables fields in two separate SQL sever databases.

Say I have table T1 in database DB1 and table T2 in database DB2.


Hope this make sense


Cheers

Shabar






Go to Top of Page

shabar
Starting Member

4 Posts

Posted - 2010-09-13 : 15:33:38
Hi

Can we implement following format to access two databases

quote:

select *
from currentDB_table cdb
, remoteDB_table@REMOTEDBLINK rdb
where cdb.field1 = rdb.field1




Cheers

Shabar
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2010-09-13 : 19:21:07
If the two databases are on the same SQL Server instance, you can simply use a three part name for the "remote" database:

select here.*
from MyLocalTable here
inner join MyOtherDatabase.dbo.MyOtherTable there
on here.MyColumn = there.MyColumn

If the other database is on another instance, you could define a linked server relationship to that database and then use a four part name (LinkedServerName.Dataqbase.Schema.Object) to access the remote item.

=======================================
In all life one should comfort the afflicted, but verily, also, one should afflict the comfortable, and especially when they are comfortably, contentedly, even happily wrong. -John Kenneth Galbraith, economist (1908-2006)
Go to Top of Page

shabar
Starting Member

4 Posts

Posted - 2010-09-14 : 06:14:57
quote:
Originally posted by Bustaz Kool

If the two databases are on the same SQL Server instance, you can simply use a three part name for the "remote" database:

select here.*
from MyLocalTable here
inner join MyOtherDatabase.dbo.MyOtherTable there
on here.MyColumn = there.MyColumn

If the other database is on another instance, you could define a linked server relationship to that database and then use a four part name (LinkedServerName.Dataqbase.Schema.Object) to access the remote item.

=======================================
In all life one should comfort the afflicted, but verily, also, one should afflict the comfortable, and especially when they are comfortably, contentedly, even happily wrong. -John Kenneth Galbraith, economist (1908-2006)




Thanks very much Bustaz Kool

It's great


Cheers

Shabar
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2010-09-14 : 17:19:25
If the two databases are on the same SQL Server instance, you can simply use a three part name for the "remote" database:

select here.*
from MyLocalTable here
inner join MyOtherDatabase.dbo.MyOtherTable there
on here.MyColumn = there.MyColumn

If the other database is on another instance, you could define a linked server relationship to that database and then use a four part name (LinkedServerName.Dataqbase.Schema.Object) to access the remote item.

=======================================
In all life one should comfort the afflicted, but verily, also, one should afflict the comfortable, and especially when they are comfortably, contentedly, even happily wrong. -John Kenneth Galbraith, economist (1908-2006)
Go to Top of Page
   

- Advertisement -