网站制作产品资料,开源网站搭建,上海网站推广珈维,北京网站制作推广MySQL自定义函数 函数与存储过程类似#xff0c;也是一组预先编译好的SQL语句的集合#xff0c;但是存储过程可以有0个或多个返回#xff0c;函数就只能有一个返回 创建函数 #语法 参数列表包含两部分 参数名和参数类型
#函数体必须有return语句 且每个sql语句后要以;结尾 所… MySQL自定义函数 函数与存储过程类似也是一组预先编译好的SQL语句的集合但是存储过程可以有0个或多个返回函数就只能有一个返回 创建函数 #语法 参数列表包含两部分 参数名和参数类型
#函数体必须有return语句 且每个sql语句后要以;结尾 所以需要使用delimiter来重新设置结束标记
#函数体中只有一句话时可以省略begin end
create function 函数名(参数列表) returns 返回值类型
begin函数体
end执行函数 select 函数名(参数列表)查看函数 show create function 函数名;删除函数 drop function 函数名;示例 delimiter $create function myfunc(class_name varchar(20)) returns int
begin declare c int default 0; #设置局部变量作为返回值select count(s.id) into c # 将查询结果赋给局部变量from class cjoin student s on c.id s.classidwhere c.name class_name;return c; #返回
end $delimiter ;
select myfunc(计算机一班);#函数调用特别提醒一下我在创建函数的时候出错了 ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable) 需要设置一下 set global log_bin_trust_function_creatorsTRUE;https://zhhll.icu/2021/数据库/关系型数据库/MySQL/基础/15.MySQL自定义函数/ 本文由 mdnice 多平台发布