当前位置:网站首页>mysql创建存储过程及函数详解
mysql创建存储过程及函数详解
2022-04-23 11:02:00 【liming89】
文章来源: 学习通http://www.bdgxy.com/
1. 存储过程
1.1. 基本语法
create procedure name ([params])
UNSIGNED [characteristics] routine_body
?params : in|out|inout 指定参数列表 代表输入与输出
routine_body: SQL代码内容,以begin ........? ?end
characteristics: 指定存储过程特性,包含5种
? ? ? ? ? ? ? ? ? ? ? ? ? ?1 DETERMINISTIC 不确定的
2 NO SQL 没有SQl语句,当然也不会修改数据
3 READS SQL DATA 只是读取数据,当然也不会修改数据
4 MODIFIES SQL DATA 要修改数据
5 CONTAINS SQL 包含了SQL语句
1.2 创建一个指定执行权限的存储过程
create DEFINER=`root`@`%` procedure name ([params])
UNSIGNED [characteristics] routine_body
DEFINER:指明谁有权力来执行。
1.3?DELIMITER 的使用
? ? ? ”DELIMITER //“ 表示设置“//”符号为结束语,因为mysql中默认语句结束为分号‘;',为了避免存储过程与mysql语句符号冲突,所以有时使用DELIMITER来改变结束语符号,要配合end //来使用;
?示例:创建一个root账户执行的存储过程,输出给定字符串的长度
DELIMITER //
CREATE definer=`root`@`%` PROCEDURE `avgFruitPrice`(
in f_string VARCHAR(200)
)
BEGIN
select length(f_string);
END//
2. 创建函数?
函数的创建与存储过程相同
示例
DELIMITER //
CREATE definer=`root`@`%` FUNCTION `my_length`(
f_string VARCHAR(200)
)
RETURNS INT(11)
UNSIGNED NO SQL
BEGIN
return length(f_string);
END//
注意: 创建函数需要注意3个地方,
1.??RETURNS: 需指定返回类型
2.?UNSIGNED ?NO SQL 需指定存储过程特性
3.return: 返回需要的数据
遇见的错误:
如报错如上信息就是没有指定存储过程特性。
存储过程函数中可以通过mysql查询结果为其参数复制: 其语句为select .... into
begin
declare onename char(50) default‘0’;
declare twoname char(50);
select f_name, b_name into onename, twoname from t_user where id =1;
…
end//
说明:
declare: 存储过程及函数内部定义变量
default:默认值
到此这篇关于mysql创建存储过程及函数详解的文章就介绍到这了,更多相关mysql存储过程及函数内容请搜索菜鸟教程www.piaodoo.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持菜鸟教程www.piaodoo.com!
版权声明
本文为[liming89]所创,转载请带上原文链接,感谢
https://blog.csdn.net/liming89/article/details/124334048
边栏推荐
- Simple thoughts on the design of a microblog database
- Mba-day6 logic - hypothetical reasoning exercises
- 一个微博数据库设计带来的简单思考
- Detailed explanation of typora Grammar (I)
- 学习 Go 语言 0x02:对切片 Slice 的理解
- Kaggle - real battle of house price prediction
- Visualization Road (11) detailed explanation of Matplotlib color
- Installing MySQL with CentOS / Linux
- CUMCM 2021-B:乙醇偶合制備C4烯烴(2)
- MBA-day5數學-應用題-工程問題
猜你喜欢
About the three commonly used auxiliary classes of JUC
The courses bought at a high price are open! PHPer data sharing
Ueditor -- limitation of 4m size of image upload component
Solutions to common problems in visualization (IX) background color
比深度学习更值得信赖的模型ART
Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)
Visual Road (XII) detailed explanation of collection class
Visual solutions to common problems (VIII) mathematical formulas
精彩回顾|「源」来如此 第六期 - 开源经济与产业投资
CUMCM 2021-B:乙醇偶合制備C4烯烴(2)
随机推荐
Gets the current time in character format
比深度学习更值得信赖的模型ART
MBA-day5数学-应用题-工程问题
Pytorch implementation of transformer
【leetcode】199. Right view of binary tree
Solutions to common problems in visualization (IX) background color
Visual Road (XII) detailed explanation of collection class
Kaggle - real battle of house price prediction
Visualization Road (11) detailed explanation of Matplotlib color
Visual common drawing (I) stacking diagram
Xdotool key Wizard
Learning Notes 6 - Summary of several deep learning convolutional neural networks
Installing MySQL with CentOS / Linux
Jupyter Lab 十大高生产力插件
@valid,@Validated 的学习笔记
Promise详解
ConstraintLayout布局
How to bind a process to a specified CPU
Is the pointer symbol of C language close to variable type or variable name?
Leetcode22: bracket generation