1

我有一个名为 dbo.Node 的表:

NodeID      int
ParentNodeID    int
Title

以下函数显示了层次结构

<cffunction name="getNodePath" returnType="string">
    <cfargument name="NodeID" required="false" default="">

    <cfset var qryNode = "">

    <cfif NOT isNumeric(arguments.nodeID)>
        <cfreturn "">
    </cfif>

    <cfquery name="qryNode" cachedWithin="#CreateTimeSpan(0,0,1,0)#">
        SELECT  ParentNodeID, Title
        FROM    dbo.Node WITH (NOLOCK)
        WHERE   NodeID = <cfqueryparam CFSQLType="CF_SQL_INTEGER" value="#arguments.NodeID#">
    </cfquery>      

    <cfreturn getNodePath(qryNode.ParentNodeID) & qryNode.Title & " &raquo; ">
</cffunction>

此代码使用 cacheWithin 是因为已知会调用许多相同的父路径。平均而言,这会从原始页面调用大约 20 次。总体而言,由于查询的递归性质,它被调用了 100 次。此外,此函数返回一个字符串。这意味着格式在模型而不是视图中。

有没有办法用 CTE 代替它?

4

1 回答 1

0

带 CTE 的版本

<cfset var qryNode = "">
<cfset var lstPath = "">

<cfif NOT isNumeric(arguments.nodeID)>
    <cfreturn QueryNew("Empty")>
</cfif>

<cfquery name="qryNode">
    WITH Family AS ( 

        SELECT  N.NodeID, ISNULL(N.ParentNodeID, '') AS ParentNodeID, Slug, N.Title, N.Kind, 0 AS Depth, N.CreateDate
        FROM    dbo.vwNode N WITH (NOLOCK)
        WHERE   NodeID = TRY_CONVERT(int, <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.NodeID#">)

        UNION ALL 

        SELECT  N2.NodeID, ISNULL(N2.ParentNodeID, '') AS ParentNodeID, N2.Slug,  N2.Title, N2.Kind, Depth + 1, N2.CreateDate
        FROM    dbo.vwNode N2 WITH (NOLOCK)

        INNER JOIN  Family 
        ON      Family.ParentNodeID = N2.NodeID
        WHERE   PrimaryRecord = 0 
        ) 


    SELECT  NodeID, ParentNodeID, Slug, Title, Kind, Depth, CreateDate
    FROM    Family
    ORDER BY Depth DESC
</cfquery>      

<cfreturn qryNode>

With语句替换了递归调用。这个函数现在只调用了大约 20 次。无需缓存结果,因为它们不会被重用。该函数现在返回一个查询。格式现在在它所属的视图上

于 2012-12-02T19:35:38.347 回答