如何在没有qoq的情况下从不同的数据库加入两个查询?

时间:2014-08-29 18:58:41

标签: coldfusion

我有两个不同的查询

  <cfquery datasource="#one#" name="GetResults">
    SELECT ID, Account, Value
      FROM Logger_Events
      left join Event_Details
      on ID =  EventID
      where type = '123'

    </cfquery>

    <cfquery datasource="#two#" name="getSubtotal">
      select account from daily 
    </cfquery>

第一个查询给我200条记录,第二条查询给我喜欢 五十万条记录。

有没有办法让第二个查询从第一个查询中获取记录, 没有查询查询? 我将使用帐户列加入。

这些表位于不同的服务器中。

3 个答案:

答案 0 :(得分:5)

这是QoQ的原因之一。您可以运行第一个查询,然后将数据转换为XML并将其作为参数传递给第二个DB服务器上的存储过程,以便直接在DB上过滤第二个查询。只需加入XML或使用XML填充临时表。

答案 1 :(得分:2)

由于您使用的是sql server,我建议使用链接服务器。选择一个服务器并将链接服务器设置为另一个服务器。然后设置一个类似这样的存储过程;

declare #TempTable table (field1 datatype1, etc);

insert into #TempTable (field1, field2, etc)
select field1, field2, etc
from openquery
(linkedServerName, 
'select field1, field2 etc
 from blah blah blah
 ')

select yourfields
from #TempTable join aTableInThatDB on etc

一开始细节有点痛苦,但效果很好。此外,互联网上有很多关于链接服务器和openquery的信息。

答案 2 :(得分:0)

我已经完成了这个函数来替换外表中包含的文本字段的表的外键。这可以使用QueryAddColumn()和QuerySetCell()将新列附加到原始查询。

这个想法是将其中一个查询转换为结构。 我的功能很基本,但目前看起来像这样:

<cffunction name="joinNameFromIDStructs" returnType="query" hint="Returns the Fund Activity">
        <cfargument name="qryMain" type="query" required="Yes" hint='The query which is going to be translated' >
        <cfargument name="foreignTable" type="string" required="Yes" hint='table which contains the details of our foreign column' >
        <cfargument name="foreignPK" type="string" required="No" default="#arguments.foreignTable#ID" hint="Primary Key in the foreign table" >
        <cfargument name="foreignKey" type="string" required="Yes" hint="column in the main table which references another table" >
        <cfargument name="foreignText" type="string" required="Yes" hint="the column in the foreing table which hold the required details. It will override the foreign key column in the main table" >

        <cfset var idPK = "">
        <cfset var qryForeignTable = "">
        <cfset var stForeignTable = StructNew()>
        <cfset var listForeignPKs = evaluate("ValueList(arguments.qryMain.#arguments.foreignKey#)")/>
        <cfif ListLen(listForeignPKs)>
            <cfquery name="qryForeignTable" datasource='#application.siteDataSource#' >
                SELECT #arguments.foreignPK#, #arguments.foreignText# FROM #arguments.foreignTable#
                    WHERE #arguments.foreignPK# IN (<cfqueryparam cfsqltype="cf_sql_integer" value='#arrayToList(listToArray(listForeignPKs))#' list="true" />) <!--- CF8 does not support ListRemoveDuplicates , hence we will use the array conversion to remove empties --->
            </cfquery>      
            <cfloop query='qryForeignTable'>
                <cfset stForeignTable[qryForeignTable[arguments.foreignPK][qryForeignTable.currentrow]] = qryForeignTable[arguments.foreignText][qryForeignTable.currentrow]/>
            </cfloop>
            <cfloop query='arguments.qryMain'>
                <cfset idPK = arguments.qryMain[arguments.foreignKey]/>
                <cfif idPK neq "">
                    <cfset arguments.qryMain[arguments.foreignKey][arguments.qryMain.currentrow] = stForeignTable[idPK]/>
                </cfif>
            </cfloop>
        </cfif>
        <cfreturn arguments.qryMain/>
</cffunction>
相关问题