当前位置:网站首页>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
边栏推荐
- The courses bought at a high price are open! PHPer data sharing
- Chapter 1 of technical Xiaobai (express yourself)
- Differences among restful, soap, RPC, SOA and microservices
- Using El popconfirm and El backtop does not take effect
- Qinglong panel pull library command update [April 20, 2022] collection is not lost
- Use of SVN:
- Xdotool key Wizard
- RESTful和SOAP的区别
- 活动进行时! 点击链接加入直播间参与“AI真的能节能吗?”的讨论吧!
- UEditor之——图片上传组件大小4M的限制
猜你喜欢
Google Earth engine (GEE) - scale up the original image (taking Hainan as an example)
精彩回顾|「源」来如此 第六期 - 开源经济与产业投资
比深度学习更值得信赖的模型ART
Mysql8. 0 installation guide
学习 Go 语言 0x04:《Go 语言之旅》中切片的练习题代码
【leetcode】199. Right view of binary tree
Deploy jar package
UEditor之——图片上传组件大小4M的限制
About the three commonly used auxiliary classes of JUC
Visualization Road (11) detailed explanation of Matplotlib color
随机推荐
学习网站资料
使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
Restful、SOAP、RPC、SOA、微服务之间的区别
Let the LAN group use the remote device
Idea - indexing or scanning files to index every time you start
Go interface usage
remote: Support for password authentication was removed on August 13, 2021.
语雀文档编辑器将开源:始于但不止于Markdown
Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area
Jupyter lab top ten high productivity plug-ins
Resolution and size of mainstream mobile phones
Source insight 4.0 FAQs
Excel · VBA custom function to obtain multiple cell values
【leetcode】199. Right view of binary tree
colab
The difference between restful and soap
ffmpeg命令行常用参数
Visualized common drawing (II) line chart
比深度学习更值得信赖的模型ART
Ueditor -- limitation of 4m size of image upload component