存储函数中的临时表?

5
我正在编写一个函数,我需要使用TABLE变量(据我所知,在MySQL中不存在),或者使用临时表。
然而,似乎临时表只能在存储过程中使用,而不是函数。我一直收到这个错误:

存储函数或触发器不允许显式或隐式提交。


我尝试构建的解决方案是我的早期问题的一部分。它是一个函数,接收一个起始日期、一个结束日期和一个逗号分隔的字符串。它首先找到起始日期和结束日期之间的所有月份,并将它们作为单独的记录保存在第一个临时表中。然后它解析逗号分隔的字符串,并将其保存到第二个临时表中。然后它在两个表上执行选择连接,如果存在记录,则返回true,否则返回false。
我的意图是将其用作另一个查询的WHERE子句的一部分,因此它必须是一个函数而不是一个存储过程。
如何在存储函数中使用临时表?如果我不能,我应该怎么做?
这是我的(当前损坏的)函数代码(或作为Gist):
-- need to parse out a string like '4,2,1' and insert values into temporary table
-- MySQL doesn't have a native string split function, so we make our own
-- taken from: http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
DROP FUNCTION IF EXISTS SPLIT_STR;
CREATE FUNCTION SPLIT_STR(x VARCHAR(255), delim VARCHAR(12), pos INT) RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos), LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1), delim, '');

-- need to find all months between the start and end date and insert each into a temporary table
DROP FUNCTION IF EXISTS months_within_range;

DELIMITER //

CREATE FUNCTION months_within_range(starts_at DATE, ends_at DATE, filter_range VARCHAR(255)) RETURNS TINYINT
BEGIN

    DROP TABLE IF EXISTS months_between_dates;
    DROP TABLE IF EXISTS filter_months;
    CREATE TEMPORARY TABLE months_between_dates (month_stuff VARCHAR(7));
    CREATE TEMPORARY TABLE filter_months (filter_month VARCHAR(7));

    SET @month_count = (SELECT PERIOD_DIFF(DATE_FORMAT(ends_at, "%Y%m"), DATE_FORMAT(starts_at, "%Y%m")));
    -- PERIOD_DIFF only gives us the one month, but we want to compare to, so add one
    -- as in, the range between 2011-01-31 and 2011-12-01 should be 12, not 11

    INSERT INTO months_between_dates (month_stuff) VALUES (DATE_FORMAT(starts_at, "%Y-%m"));
    SET @month_count = @month_count + 1;
    -- start he counter at 1, since we've already included the first month above
    SET @counter = 1;
    WHILE @counter < @month_count DO
        INSERT INTO months_between_dates (month_stuff) VALUES (DATE_FORMAT(starts_at + INTERVAL @counter MONTH, "%Y-%m"));
        SET @counter = @counter + 1;
    END WHILE;

    -- break up the filtered string
    SET @counter = 1;
    -- an infinite loop, since we don't know how many parameters are in the filtered string
    filters: LOOP
        SET @filter_month = SPLIT_STR(filter_range, ',', @counter);
        IF @filter_month = '' THEN LEAVE filters;
        ELSE
            INSERT INTO filter_months (filter_month) VALUES (@filter_month);
            SET @counter = @counter + 1;
        END IF;
    END LOOP;

    SELECT COUNT(*) INTO @matches FROM months_between_dates INNER JOIN filter_months ON months_between_dates.month_stuff = filter_months.filter_month;

    IF @matches >= 1 THEN RETURN 1;
    ELSE RETURN 0;

END//

DELIMITER ;
1个回答

10

在MySQL函数中,DROP TABLE语句会导致隐式提交,这是不允许的。但是DROP TEMPORARY TABLE不会导致提交。如果您不担心已命名为months_between_dates或filter_months的普通(非临时)表存在,则应该能够更改。

DROP TABLE IF EXISTS months_between_dates;
DROP TABLE IF EXISTS filter_months;

DROP TEMPORARY TABLE IF EXISTS months_between_dates;
DROP TEMPORARY TABLE IF EXISTS filter_months;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接