CF查询两个不同的数据库以匹配名称为

时间:2015-10-26 16:46:32

标签: sql coldfusion

我在不同的服务器上有两个数据库,我需要组合两个表来显示ID的名称。如何同时查询两个不同的数据库?

datasource="DB1, DB2" ?

所以我在这里查询如何将数据库和表格结合起来,以便将op_idassoc_name相匹配?

    <cfquery name="processTable" datasource="DB1, DB2">
        SELECT *
        FROM [DB1].dbo.Dealer_Track_Work, [DB1].dbo.Dealer_Track_Dealers, [DB2].dbo.tco_associates
        WHERE [DB1]dbo.Dealer_Track_Work.dealerID = [DB1].dbo.Dealer_Track_Dealers.id,
              [DB1].dbo.Dealer_Track_Work.op_id = [DB2].dbo.Dealer_Track_Dealers.assoc_name,
              [DB1].dbo.Dealer_Track_Work.Closed_by = [DB2]dbo.Dealer_Track_Dealers.assoc_name      
    </cfquery>

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

<CFQUERY NAME="getDetailsQuery1" DATASOURCE="DB1"> 
 SELECT * FROM getDetailsA 
</CFQUERY>



<CFQUERY NAME="getDetailsQuery2" DATASOURCE="DB2"> 
 SELECT * FROM getDetailsB 
</CFQUERY> 


<CFQUERY NAME="getDetails" DBTYPE="query"> 
 SELECT * FROM getDetailsQuery1 
 UNION 
 SELECT * FROM getDetailsQuery2 
 ORDER BY Name ASC 
</CFQUERY> 

在这里你拥有一切......

<CFQUERY NAME="getDetailsWhere" DBTYPE="query"> 
 SELECT * FROM getDetails where id = '123'
</CFQUERY>

功能:

<cffunction name="qDisplay" output="yes" access="public" returntype="query">
<cfargument name="id"   type="string"   required="yes"  />

<CFQUERY NAME="getDetailsQuery1" DATASOURCE="DB1"> 
     SELECT * FROM getDetailsA 
    </CFQUERY>



<CFQUERY NAME="getDetailsQuery2" DATASOURCE="DB2"> 
 SELECT * FROM getDetailsB 
</CFQUERY> 


<CFQUERY NAME="getDetails" DBTYPE="query"> 
 SELECT * FROM getDetailsQuery1 
 UNION 
 SELECT * FROM getDetailsQuery2 
 ORDER BY Name ASC 
</CFQUERY> 


<CFQUERY NAME="getDetailsWhere" DBTYPE="query"> 
 SELECT * FROM getDetails where id = #arguments.id#
</CFQUERY>

<cfreturn getDetailsWhere>
</cffunction>

希望这有帮助!