跨服务器、数据库、表联合查询

 

ExpandedBlockStart.gif 代码
SELECT  I. * , M. *   FROM   OPENROWSET ( ' SQLOLEDB ' ' 192.168.10.2 ' ' sa ' ' 123 ' [ Database1 ] . [ dbo ] . [ Table1 ] ) I  INNER   JOIN  (
SELECT   DISTINCT  P.IPSAppID AppId, P.DepartmentId, C.CategoryId, T.Creator  FROM   OPENROWSET ( ' SQLOLEDB ' ' 192.168.10.1 ' ' sa ' ' 123 ' [ Database2 ] . [ dbo ] . [ table2 ] ) P  LEFT   JOIN  (
SELECT   DISTINCT   LTRIM (DepartmentId) DepartmentId, UserName Creator  FROM   OPENROWSET ( ' SQLOLEDB ' ' 192.168.10.1 ' ' sa ' ' 123 ' [ Database3 ] . [ dbo ] . [ view1 ] WHERE  UserName  IN  
(
SELECT   DISTINCT  Creator  FROM   OPENROWSET ( ' SQLOLEDB ' ' 192.168.10.1 ' ' sa ' ' 123 ' [ Database1 ] . [ dbo ] . [ Table1 ] WHERE  AppId  =   ' 8cefca00-9733-4976-951a-e19346603717 '   AND  CategoryId  =   ' b14b4852-c0e4-4db6-a91e-704b699a8fa2 ' ) ) T 
ON  P.DepartmentId  =  T.DepartmentId 
LEFT   JOIN   OPENROWSET ( ' SQLOLEDB ' ' 192.168.10.1 ' ' sa ' ' 123 ' [ Database2 ] . [ dbo ] . [ Table3 ] ) C  ON  C.AppId  =  P.IPSAppID 
WHERE  P.ClassId  IS   NULL   AND  T.Creator  IS   NOT   NULL   AND  C.CategoryName  =   ' activity ' ) M 
ON  M.Creator  =  I.Creator 
WHERE  I.AppId  =   ' 8cefca00-9733-4976-951a-e19346603717 '   AND  I.CategoryId  =   ' b14b4852-c0e4-4db6-a91e-704b699a8fa2 '

 

 

转载于:https://www.cnblogs.com/Dicky/archive/2010/05/12/Cross-Server-Database-Table-Query.html

你可能感兴趣的:(跨服务器、数据库、表联合查询)