从不同的表和不同数据库中选择特定的列(selecting specific columns from

2019-10-17 12:36发布

我有3个数据库

  - ECPNWEB
  - ECPNDB
  - ECPNPOS

而且3个表

  - account -> ECPNWEB
  - branch -> ECPNDB
  - terminal -> ECPNPOS

和具有多个列的表

  - account (accntid,managedby,account,first,last,....)
  - branch(branch_code,company_code,name,distributor,cutoff,....)
  - terminal(retail,brand,model,serial,status,....)

所有我想要做的是让特定的列,我需要的输出是:

 managedby for table account and ECPNWEB database
 name for table branch and ECPNDB database
 model for table terminal and ECPNPOS database..

像这样

 |managedby|name|model

提前致谢

Answer 1:

  1. sp_addlinkedserver()是你所需要的
  2. exec sp_addlinkedserver @server = '*server_name*' // ---更换*服务器名*与你
  3. 它看起来是这样的:例如select * from [server].[database].[schema].[table]
  4. 看看这个链接http://msdn.microsoft.com/en-us/library/ms190479.aspx

我不是这件事,但是试试这个,告诉我什么是O / P。 例如,你可以试试这个方法

SELECT firstdb.*, seconddb.*
FROM Server1.Database1.dbo.myTable AS firstdb
INNER JOIN Server2.Database2.dbo.myTable AS seconddb
   ON firstdb.id = seconddb.id

EDIT2:试试这个,不知道这将要给你期望的结果,没有测试...取代*列名*与你

SELECT account.managedby, branch.name, terminal.model FROM ECPNWEB.dbo.account, ECPNDB.dbo.branch, ECPNPOS.dbo.terminal AS a INNER JOIN account ON account.(column_name)=branch.(column_name)  INNER JOIN terminal ON account.(column_name)=terminal.(column_name)


文章来源: selecting specific columns from different tables and different database