June 12, 2009 at 12:08 pm
I have to write a query from 2 different databses on 2 different servers linking up bothe by an id in respective tables.How can i do this.
Thank you,
s
June 12, 2009 at 12:21 pm
using :
1)Linked server or
2)Openrowset
June 12, 2009 at 12:25 pm
You have two servers, local and remote. The remote server (or instance) must be a linked server:
From the current server write this:
SELECT a.Filed1, b.Field2
FROM LocalTable a
JOIN LinkedServer.OneDB.dbo.RemoteTable b
ON a.Id = b.Id
Translating LinkedServer.OneDB.dbo.RemoteTable
LinkedServer = Name of the linked server
OneDB = name of the database in the linked server
dbo = schema name
RemoteTable = name of the table in the linked server
Alberto De Rossi
Microsoft Certified Solutions Associate - SQL Server
Microsoft MVP - Data Platform
Power BI User Group Lima - Community Leader
June 12, 2009 at 3:24 pm
You have two servers, local and remote. The remote server (or instance) must be a linked server:
From the current server write this:
SELECT a.Filed1, b.Field2
FROM LocalTable a
JOIN LinkedServer.OneDB.dbo.RemoteTable b
ON a.Id = b.Id
Translating LinkedServer.OneDB.dbo.RemoteTable
LinkedServer = Name of the linked server
OneDB = name of the database in the linked server
dbo = schema name
RemoteTable = name of the table in the linked server
You might also:
select id, field into #tmp
from LinkedServer.OneDB.dbo.RemoteTable
where (just the records you need)
--and then
select a.field, b.field
from LocalTable a join #tmp b
on a.id = b.id
Randy
June 16, 2009 at 9:43 am
Thnanks for the reply.But when i try to write the following it gives me error as my server name is with a slash. Can i use ip adress instead of server name.
SELECT a.name,b.desc
FROM dbo.tbl1 a
JOIN SA_SB_SC\sa_sb_sc.Reports.dbo.tbl2 b
ON a.id = b.id
Please advise.
--sh
June 16, 2009 at 9:45 am
shipra20 (6/16/2009)
Thnanks for the reply.But when i try to write the following it gives me error as my server name is with a slash. Can i use ip adress instead of server name.SELECT a.name,b.desc
FROM dbo.tbl1 a
JOIN SA_SB_SC\sa_sb_sc.Reports.dbo.tbl2 b
ON a.id = b.id
Please advise.
--sh
JOIN [SA_SB_SC\sa_sb_sc].Reports.dbo.tbl2 b
should do the trick.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply