创建一个在mysql中返回表值的函数

时间:2018-04-10 11:24:25

标签: mysql

大家好我在Mysql中创建函数时遇到了问题。

1.我必须创建一个函数,它将获得一个输入字符串,该字符串由逗号分隔值的日期组成,我想通过函数将其转换为多个日期记录

请找到以下示例输入

set @value = '01-02-2017,01-03-2017,04-03-2017,07-03-2017,13-03-2017,14-03-2017,17-03-2017,18-03-2017,31-03-2017';

我尝试过的查询如下所示

delimiter $$
create function fn_split(value varchar(255)) 
returns int
begin
declare num int;
declare val int;
set num := 1;
set val := (select char_length(value)-char_length(replace(value,',','')));
create temporary table if not exists my_tab(date1 datetime);
delete from my_tab;
while num>val
insert into mytab (date1)
select SUBSTRING_INDEX(value,',',num);
set num = num + 1;
end while;
select count(date1) into count1 from my_tab;
return (count1);
end $
delimiter;

我得到的错误

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into mytab (date1) select SUBSTRING_INDEX(value,',',num); set num = num +' at line 11

提前致谢。

0 个答案:

没有答案