您的位置:首页 > 数据库 > MySQL

mysql函数详细说明

2014-08-14 00:00 274 查看
摘要: mysql函数详细说明

老是喜欢忘记以前的mysql语法;

# 开头定义所属用户、函数名称、参数、返回值、编码
CREATE DEFINER=`admin`@`%` FUNCTION `gethotwordsbyconsultid`(`p_id` int) RETURNS varchar(5000) CHARSET utf8
BEGIN
#定义返回值
declare v_result varchar(1000) default '';
#定义临时变量
declare v_tmp varchar(150) default '';
#定义游标是否循环完毕,0表示false,默认没有循环完毕
declare done int default 0;
#定义游标
declare v_cur cursor for select a.wordsName from t_help_hotwords a inner join t_help_hot_consult b on a.id = b.hotId and b.consultId = p_id;
#定义游标完成,1表示true,代表循环完毕,这里用exception的handler实现
declare continue handler for not found set done = 1;
#打开游标
open v_cur;
#开始循环
repeat
#填值
fetch v_cur into v_tmp;
#这里not done代表not 0,也就是not false,即true。
if not done then
#设置值,拼接字符串
set v_result = concat(v_result,',',v_tmp);
end if;
#这里只有done为1,也就是true的时候终止循环
until done end repeat;
#关闭游标
close v_cur;
#返回值
return v_result;
END
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息