跳至主要內容

存储函数

Jin小于 1 分钟

存储函数

1、介绍

存储函数是有返回值的存储过程,存储函数的参数只能是IN类型的。具体语法如下:

characteristic说明:

  • DETERMINISTIC:相同的输入参数总是产生相同的结果

  • NO SQL :不包含 SQL 语句。

  • READS SQL DATA:包含读取数据的语句,但不包含写入数据的语句。

2、案例

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

-- 存储函数
-- 从1到n的累加

create function fun1(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;
select fun1(50);

mysql8.0版本中binlog默认是开启的,一旦开启了,mysql就要求在定义存储过程时,需要指定

characteristic特性,否则就会报如下错误:

[HY000][1418] 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)
image-20220531225333188
image-20220531225333188
贡献者: Jin