sql - Querying data by joining two tables in two database on different servers -


there 2 tables in 2 different databases on different servers, need join them make few queries. options have? should do?

you'll need use sp_addlinkedserver create server link. see reference documentation usage. once server link established, you'll construct query normal, prefixing database name other server. i.e:

-- db1 select * [mydatabaseondb1].[dbo].[mytable] tab1     inner join [db2].[mydatabaseondb2].[dbo].[myothertable] tab2         on tab1.id = tab2.id 

once link established, can use openquery execute sql statement on remote server , transfer data you. can bit faster, , let remote server optimize query. if cache data in temporary (or in-memory) table on db1 in example above, you'll able query joining standard table. example:

-- fetch data other database server select * #mytemptable openquery([db2], 'select * [mydatabaseondb2].[dbo].[myothertable]')  -- can join temp table see data select * [mydatabaseondb1].[dbo].[mytable] tab1     inner join #mytemptable tab2 on tab1.id = tab2.id 

check out documentation openquery see more examples. example above pretty contrived. use first method in specific example, second option using openquery can save time , performance if use query filter out data.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -