一、基本语法
delimiter 自定义符号 -- 如果函数体只有一条语句, begin和end可以省略, 同时delimiter也可以省略
create function 函数名(形参列表) returns 返回类型 -- 注意是retruns
begin
函数体 -- 函数内定义的变量如:set @x = 1; 变量x为全局变量,在函数外面也可以使用
返回值
end
自定义符号
delimiter ;
二、示例
delimiter $$
create function test(ia int, ib int) returns int
begin
return ia + ib;
end
$$
delimiter ;
三、查看函数
1. show function status [like 'pattern']; -- 查看所有自定义函数, 自定义函数只能在本数据库使用。
2. show create function 函数名; -- 查看函数创建语句
四、删除函数
drop function 函数名;
五、综合应用
实例一:
目前数据库中有现成的一张分数表,就拿来测试了。
表:
insert into TB_SCORE (id, userid, subject, score) values (1, '001