mysql自定义函数,存储过程

自定义函数参考
自定义存储过程参考
1.函数与存储过程的区别:函数只会返回一个值,不允许返回一个结果集。函数强调返回值,所以函数不允许返回多个值的情况,即使是查询语句。

复制语法:
create function 函数名([参数列表]) returns 数据类型
begin
 sql语句;
 return 值;
end;
复制例子(无惨):
create function myDate()
returns varchar(30)
comment '我的日期函数'
return date_format(now(),'%Y年%m月%d日 %H时%i分%s秒');

复制例子(有参):
create function myAVG(num1 int,num2 int)
returns float(10,2) 
comment '计算两个数的均值'
return (num1+num2)/2;

复制复合结构(例子):
create function mygetMax(num1 int, num2 int)
        returns int  
	comment '求最大值'						
    begin
        declare res int;
        if(num1 > num2) then
            set res = num1;
        elseif (num1 < num2) then
            set res = num2;
        else
            set res = num1;
        end if;
        return res;
    end;

posted @   kht  阅读(81)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
点击右上角即可分享
微信分享提示