当前位置:网站首页>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
边栏推荐
- Installing MySQL with CentOS / Linux
- Which company is good for opening futures accounts? Who can recommend several safe and reliable futures companies?
- Visualized common drawing (II) line chart
- Visual common drawing (V) scatter diagram
- 全栈交叉编译X86完成过程经验分享
- 活动进行时! 点击链接加入直播间参与“AI真的能节能吗?”的讨论吧!
- Precautions for latex formula
- 使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
- VIM + ctags + cscope development environment construction guide
- Differences among restful, soap, RPC, SOA and microservices
猜你喜欢

Reading integrity monitoring techniques for vision navigation systems - 5 Results

Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing

Source insight 4.0 FAQs

vm设置静态虚拟机

JVM - common parameters

Notes on concurrent programming of vegetables (V) thread safety and lock solution

MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题

Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)

SVN的使用:

Visual common drawing (III) area map
随机推荐
MBA-day5数学-应用题-工程问题
Dirichlet prefix sum (number theory optimization formula sub complexity weapon)
Google Earth engine (GEE) - scale up the original image (taking Hainan as an example)
学习 Go 语言 0x06:《Go 语言之旅》中 斐波纳契闭包 练习题代码
Resolution and size of mainstream mobile phones
MBA-day5数学-应用题-工程问题
妊娠箱和分娩箱的区别
Problems of class in C # and database connection
Microsoft Access database using PHP PDO ODBC sample
CUMCM 2021-b: preparation of C4 olefins by ethanol coupling (2)
colab
Detailed explanation of typora Grammar (I)
Facing the global market, platefarm today logs in to four major global platforms such as Huobi
Notes on concurrent programming of vegetables (IX) asynchronous IO to realize concurrent crawler acceleration
全栈交叉编译X86完成过程经验分享
Visual solutions to common problems (VIII) mathematical formulas
Is the pointer symbol of C language close to variable type or variable name?
How to use JDBC callablestatement The wasnull () method is called to check whether the value of the last out parameter is SQL null
使用zerotier让异地设备组局域网
Go interface usage