小樱知识 > 生活常识mysql存储过程和函数区别(附举例详细mysql存储过程)

mysql存储过程和函数区别(附举例详细mysql存储过程)

提问时间:2022-03-16 14:25:25来源:小樱知识网


mysql存储过程中局部变量定义在代码最前面,作用范围也是在此函数范围内。注意同全局变量和用户变量区分开。

使用declare关键字来声明变量

注意必须写在代码块的最前面

de count int DEFAULT 0;DECLARE username varchar(32);

一次声明多个相同类型的变量

DECLARE p_parent int DEFAULT 0;DECLARE parent0, parent1, parent2, parent3, parent4, parent5 int DEFAULT 0;

使用set来给变量赋值

set <变量名> = <值>

 DECLARE sflag int default 0; set sflag =100;

select 语句给变量赋值

select 列名 into 变量名,支持多个变量同时赋值。

SELECT col_name[, …] INTO var_name[,…] from table

SELECT IFNULL(cnt,0) into parent0 from temp_student where b = 0 limit 1;
select sname, sno into p_name, p_no from temp_student where id=888;select 'result:', p_no, p_name;

游标赋值

DECLARE p_id int;DECLARE p_name varchar(32);DECLARE sflag int DEFAULT 0;DECLARE cur_school CURSOR FOR select id,name from school where status=0;DECLARE CONTINUE HANDLER FOR NOT FOUND SET sflag = 1;open cur_school;fetch cur_school into p_id, p_name;  while sflag<>1 do             // ......      fetch cur_school into p_id, p_name;        select 'in while:',p_id, sflag;end while;close cur_school;

以上内容就是为大家推荐的mysql存储过程和函数区别(附举例详细mysql存储过程)最佳回答,如果还想搜索其他问题,请收藏本网站或点击搜索更多问题

内容来源于网络仅供参考
二维码

扫一扫关注我们

版权声明:所有来源标注为小樱知识网www.xiaoyin02.com的内容版权均为本站所有,若您需要引用、转载,只需要注明来源及原文链接即可。

本文标题:mysql存储过程和函数区别(附举例详细mysql存储过程)

本文地址:https://www.xiaoyin02.com/shcs/121415.html

相关文章