<cfqueryparam>显示“?”而不是数字

时间:2015-07-29 07:22:25

标签: sql-server coldfusion cfqueryparam

这是我的冷冻代码:

    SELECT
        MIN(legacy_sale_id) AS legacy_sale_id ,
        cc_id                   ,  
        сс_name                 ,
        sales_rep_name                        ,
        original_sale_id                      ,
        ls.id           ,
        <cfif ARGUMENTS.is_export neq 0>
            CASE site_id 
              WHEN 4 THEN 'EU' 
            END as site_id, 
        <cfelse>
        site_id                               ,
        </cfif>
        gp_order                              ,
        SUM(item_price_usd      * qty) AS value_usd,
        SUM(item_price_original * qty) AS value_original,
        co.total_sales_orders
    FROM
        ls
    LEFT JOIN  cc
     ON
        cc.company_id = ls.id
    LEFT JOIN
        (
            SELECT
                 company_id,
                COUNT(ll.id) AS total_sales_orders
            FROM
                ls ll
            WHERE
                cc_id IS NOT NULL
            AND site_id IN (1,4)  
            GROUP BY
                cc_id
        ) co
    ON
        co.company_id = ls.cc_id
    WHERE
        cc.deleted_timestamp IS NULL
    <cfif ARGUMENTS.p_sales_order EQ "">
        <cfif ARGUMENTS.p_date_type EQ "0">
            AND when_created BETWEEN '#ARGUMENTS.p_date_from# 00:00:00' AND '#ARGUMENTS.p_date_to# 23:59:59'
        <cfelse>
            AND when_shipped BETWEEN '#ARGUMENTS.p_date_from# 00:00:00' AND '#ARGUMENTS.p_date_to# 23:59:59'
        </cfif>
    </cfif>
    AND 
        site_id IN (#Replace("'" & ARGUMENTS.p_sites & "'", ",", "','", "ALL")#)
    AND 
        ls.sage_roles_user_id IN (#Replace("'" & ARGUMENTS.p_user_id & "'", ",", "','", "ALL")#)
    <cfif lst_exclude NEQ "">
        AND
         (
              cc_id IS NULL
            OR cc_id NOT IN (#Replace("'" & lst_exclude & "'", ",", "','", "ALL")#)
            )
    </cfif> 

    <cfif lst_custnmbr NEQ "">
        AND
            (
                gp_custnmbr IS NULL
             OR gp_custnmbr NOT IN (#Replace("'" & lst_custnmbr & "'", ",", "','", "ALL")#)
            )
    </cfif>
   <cfif ARGUMENTS.p_customer_name NEQ "">
    AND ls.сс_name LIKE <cfqueryparam value="%#ARGUMENTS.p_customer_name#%" cfsqltype="cf_sql_varchar">
   </cfif> 
   <cfif ARGUMENTS.p_sales_order NEQ "">
    AND ls.gp_order = <cfqueryparam value="#ARGUMENTS.p_sales_order#" cfsqltype="cf_sql_integer">
   </cfif>

       <cfif IsDefined('ARGUMENTS.p_country') AND ARGUMENTS.p_country NEQ "">
        AND ls.country_id = <cfqueryparam value="#ARGUMENTS.p_country#" cfsqltype="cf_sql_integer">
       </cfif>

    GROUP BY
        сс_name,
        sales_rep_name       ,
        ls.id,
        original_sale_id     ,
        site_id              ,
        gp_order,
        cc_id,
        total_sales_orders

这个coldfusion代码生成的sql是下一个:

SELECT
    *
FROM
    ls
SELECT
    MIN(legacy_sale_id) AS legacy_sale_id ,
    cc_id ,
    сс_name ,
    sales_rep_name ,
    original_sale_id ,
    ls.id ,
    CASE site_id
        WHEN 4
        THEN 'EU'
    END AS site_id,
    gp_order ,
    SUM(item_price_usd * qty)      AS value_usd,
    SUM(item_price_original * qty) AS value_original,
    co.total_sales_orders
FROM
    ls
LEFT JOIN
     cc
ON
    cc.company_id = ls.id
LEFT JOIN
    (
        SELECT
            cc_id      AS company_id,
            COUNT(ll.id) AS total_sales_orders
        FROM
            ls ll
        WHERE
            cc_id IS NOT NULL
        AND site_id IN (1,4)
        GROUP BY
            cc_id ) co
ON
    co.company_id = ls.cc_id
WHERE
    cc.deleted_timestamp IS NULL
AND when_created BETWEEN '07/28/2015 00:00:00' AND '07/29/2015 23:59:59'
AND site_id IN ('1',
                '4')
AND ls.sage_roles_user_id IN ('33333',
                                       '111111',
                                       '222222',
                                       )
AND (
        cc_id IS NULL
    OR  cc_id NOT IN ('000000',
                                    '111111',
                                    '222222',
                                    ) )
AND ls.country_id = ?
GROUP BY
    сс_name,
    sales_rep_name ,
    ls.id,
    original_sale_id ,
    site_id ,
    gp_order,
    cc_id,
    total_sales_orders

我无法理解为什么cfqueryparam返回“?”而不是数字:“ls.country_id =?” 附: Tnx用于注意,问题解决了

1 个答案:

答案 0 :(得分:8)

因为当您使用<cfqueryparam>时,您告诉CF将值作为参数传递,而不是将其硬编码到SQL字符串中。这就是它的全部目的!

<cfqueryparam>标记在SQL语句中的定位纯粹是为了标记值的占位符(?)应该去的位置。然后,SQL服务器知道在执行查询时匹配它已传递给SQL语句中占位符的参数。