如何将对象(从MySQL临时表)返回到Coldfusion存储过程?

时间:2012-06-16 10:58:09

标签: mysql stored-procedures coldfusion resultset temp-tables

我在Coldfusion8 / MySQL中调用存储过程,它从产品表中获得3种最小和最大价格。

我遇到了将临时表恢复回MySQL的问题。以下代码仅返回第一个 foundMin 值,而不返回临时表本身。

如果我在MySQL中运行它,结果是

foundmin 1st price > returned to Coldfusion
foundmax 1st price
foundmin 2nd price
foundmax 2nd price
foundmin 3rd price
foundmax 3rd price
temporary table

所以当我只想要表时,我将返回所有单独的表条目和表格。

这是我的代码:

BEGIN
    DECLARE filterILN  vARCHAR(100);
    DECLARE localILN   vARCHAR(100);
    DECLARE orderILN   vARCHAR(55);
    #search strings
    DECLARE p_e        vARCHAR(55) DEFAULT 'art.preis_ek';
    DECLARE p_a        vARCHAR(55) DEFAULT 'art.preis_aktuell';
    DECLARE p_r        vARCHAR(55) DEFAULT 'art.rabatt';
    DECLARE strLen     INT DEFAULT 4;
    DECLARE strCount   INT DEFAULT 1;
    DECLARE searchFor  vARCHAR(55);
    DECLARE foundMin     DECIMAL(12,2);
    DECLARE foundMax   DECIMAL(12,2);

    # temp table
    DROP TEMPORARY TABLE IF EXISTS MinMax;
    CREATE TEMPORARY TABLE MinMax (
        price  vARCHAR(50) DEFAULT ''
      , minVal DECIMAL(12,2) DEFAULT 0.00      
      , maxVal DECIMAL(12,2) DEFAULT 0.00    
    ) ENGINE=MEMORY;

    # FILTER 1
    IF param_reference_iln = 'A' THEN SET filterILN = 'B'
    ELSEIF param_reference_iln = 'C' THEN SET filterILN = 'D'
    END IF;

    # FILTER 2
    IF param_filter IS NOT NULL AND param_filter != ""
    THEN SET localILN = CONCAT('AND (iln = "', param_filter, '")');
    ELSE SET localILN = '*';
    END IF;

    # FILTER 3
    IF param_preorder = 'ja'
    THEN SET orderILN = CONCAT('AND vororder = "',param_preorder, '"');
    ELSE SET orderILN = '*';
    END IF;

    #loop over strIDs
    getPrice:
      LOOP
        IF ELT(strCount, p_e, p_a, p_r) = 'art.rabatt'
        THEN SET searchFor = 'art.preis_ek - art.preis_aktuell)/art.preis_ek';
        ELSE SET searchFor = ELT(strCount, p_e, p_a, p_r);
        END IF;

        #min
        SELECT MIN(searchFor) AS foundMin
        FROM artikelstammdaten AS art
           WHERE art.aktiv = "ja"
           AND art.bestand != "0"
           AND filterILN
           AND art.modus = CONCAT('OPEN ', param_unlocked_iln)
           AND localILN
           AND orderILN
           LIMIT 1;
        #max
        SELECT MAX(searchFor) AS foundMax
        FROM artikelstammdaten AS art
           WHERE art.aktiv = "ja"
           AND art.bestand != "0"
           AND filterILN       
           AND art.modus = CONCAT('OPEN ', param_unlocked_iln)
           AND localILN
           AND orderILN
           LIMIT 1;

        # insert into temp table
        INSERT INTO MinMax ( price, minVal, maxVal )
        VALUES( ELT(strCount, p_e, p_a, p_r), foundMin, foundMax );

        # increate counter by 1, end if strLen reached
        SET strCount = strCount+1;
        IF strCount = strLen
        THEN LEAVE getPrice;
        END IF;

      END LOOP getPrice;

    #output table
    SELECT * FROM MinMax;

    #destroy
    DROP TABLE MinMax;

END

正确计算这些值,并将其插入临时表中。唯一的问题是以上返回表条目和表。

问题
我如何只返回临时表作为结果集/结构,然后我可以在Coldfusion中使用它?

3 个答案:

答案 0 :(得分:2)

IMO真正的问题是你的mySQL程序返回多个结果集(不是结构)。虽然在某些情况下这可能很有用,但您的程序是无意中执行的。由于您未在 CF代码中使用这些结果,因此更好的解决方案是消除它们并仅返回单个结果。即没有必要浪费资源来返回你不需要的数据。

您的过程返回多个结果的原因是因为循环中的SELECT语句。每个SELECT生成一个单独的结果集(即查询对象)。您可以通过删除两个SELECT语句来删除它们,并将MIN / MAX值直接插入临时表中:

    INSERT INTO MinMax ( price, minVal, maxVal )
    SELECT ELT(strCount, p_e, p_a, p_r), MIN(searchFor), MAX(searchFor)
    FROM   artikelstammdaten AS art
    WHERE  art.aktiv = "ja"
       AND art.bestand != "0"
       AND filterILN
       AND art.modus = CONCAT('OPEN ', param_unlocked_iln)
       AND localILN
       AND orderILN

然后该过程应生成单个结果集,即SELECT * FROM MinMax;

答案 1 :(得分:0)

我相信没有机制让过程/函数返回整个表(TABLE不是AFAIK类型)。另一方面,只要您在同一个会话中(〜在同一个数据库连接上),您可以在程序完成后立即从您的应用程序SELECT * FROM MinMax

答案 2 :(得分:0)

知道了。我需要在Coldfusion中使用 resultset 属性,如下所示:

<!--- my stored proc --->
<cfstoredproc procedure="proc_search_select_minmax" datasource="db">
    <cfprocparam type="in" value="#A#" cfsqltype="cf_sql_varchar" maxlength="13">
    <cfprocparam type="in" value="#B#" cfsqltype="cf_sql_varchar" maxlength="13">
    <cfprocparam type="in" value="#C#" cfsqltype="cf_sql_text">
    <cfprocparam type="in" value="#D#" cfsqltype="cf_sql_char" maxlength="4">
    <!--- 7 results (3 min/3 max/temp table, I need to get the 7th resultset --->
    <cfprocresult name="min_max_struct" resultSet=7>
</cfstoredproc>

因此可以从MySQL输出结构!你只需要知道它在哪里......