和存储过程类似,是一组预先变异好的SQL语句的集合
区别:
存储过程:可有0个或多个返回。适合做批量插入、批量更新函数:有且仅有一个返回。适合做处理数据后返回一个结果注意:
参数列表包含两部份:参数名+参数类型函数体:肯定会有return语句,如果没有会报错。如果return语句没有放在函数体的最后,也不报错,但是不建议。函数体中仅有一句话,则可以省略 Begin end使用delimiter语句设置结束标记直接在 Navicat 里面创建函数:https://jingyan.baidu.com/article/09ea3ede5a0a37c0afde396e.html
查看:show create function 函数名 删除: drop function 函数名
问题:返回公司员工个数
create function myf1() returns int begin declare c int default 0; #定义变量 select count(*) into c #为变量赋值 from employee; return c; End $注意 这里创建函数时可能会遇到错误: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_creators=TRUE;
创建完后 点开myf1函数,显示具体的内容
