mysql系列XII-存储函数

存储函数是有返回值的存储过程,存储函数的参数只能是IN类型的。

1
2
3
4
5
6
CREATE FUNCTION 存储函数名称 ([ 参数列表 ])
RETURNS type [characteristic ...]
BEGIN
-- SQL语句
RETURN ...;
END ;

characteristic说明:

  • DETERMINISTIC:相同的输入参数总是产生相同的结果
  • NO SQL :不包含 SQL 语句。
  • READS SQL DATA:包含读取数据的语句,但不包含写入数据的语句。

案例

计算从1累加到n的值,n为传入的参数值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
drop function if exists f1;
create function f1(n int)
returns int deterministic
begin
declare total int default 0;
while n>0 do
set total := total +n;
set n := n - 1;
end while;
return total;
end;


mysql> select f1(10);
+--------+
| f1(10) |
+--------+
| 55 |
+--------+
1 row in set (0.00 sec)

在mysql8.0版本中binlog默认是开启的,一旦开启了,mysql就要求在定义存储过程时,要指定characteristic特性,否则就会报如下错误: