mysql – 存储函数中的临时表?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mysql – 存储函数中的临时表?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3242字,纯文字阅读大概需要5分钟。
内容图文
我正在编写一个函数,我需要使用(I hear they don’t exist in MySQL)的TABLE变量或临时表.
但是,似乎临时表似乎只在存储过程中工作,而不是函数.我一直收到这个错误:
Explicit or implicit commit is not allowed in stored function or
trigger.
我正在尝试构建的是an earlier question of mine的解决方案.它是一个接收开始日期,结束日期和逗号分隔字符串的函数.它首先查找开始日期和结束日期之间的所有月份,并将它们作为单个记录保存在第一个临时表中.然后它解析出逗号分隔的字符串并将它们保存到第二个临时表中.然后它对两者进行选择连接,如果存在记录,则返回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 ;
解决方法:
drop table语句导致隐式提交,这在mysql函数中是不允许的. drop临时表不会导致提交.如果您不担心名为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;
内容总结
以上是互联网集市为您收集整理的mysql – 存储函数中的临时表?全部内容,希望文章能够帮你解决mysql – 存储函数中的临时表?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。