Home Database Mysql Tutorial mysql---存储过程_MySQL

mysql---存储过程_MySQL

Jun 01, 2016 pm 01:08 PM

了解存储过程之前,先了解一下mysql的控制结构。

类似C语言(if……else、while循环等)SQL也有自己的控制结构。

if……else控制结构:

例如:

(1)

1

<span style="font-family:FangSong_GB2312;">if 判断表达式    then 执行语句;end if;与c语言进行比较if(判断表达式)   执行语句;</span>

Copy after login

(2)

1

<span style="font-family:FangSong_GB2312;">if 判断表达式1    then 执行语句1;else   then  执行语句2;end if;与c语言进行比较if(判断表达式1)   执行语句1;else    执行语句2;</span>

Copy after login

(3)

1

<span style="font-family:FangSong_GB2312;">if 判断表达式1    then 执行语句1;elseif 判断表达式2    then  执行语句2;&hellip;&hellip;elseif 判断表达式N    then 执行语句N;else   执行语句N+1;end if;与c语言进行比较if(判断表达式1)   执行语句1;else if(判断表达式2)   执行语句2;&hellip;&hellip;else if(判断表达式N)   执行语句N;else   执行语句N+1;</span>

Copy after login

需要注意所有的执行语句和end if都要以‘;’结束,而且判断表达式之后接then,还有一点与C语言不同的是elseif之间没有空格。

mysql中还有一些与if相关的函数

if(判断表达式,值1,值2) 如果表达式为“true”返回“值1”,表达式为“false”返回“值2”。类似于C语言中的三目运算符。

ifnull(表达式1,表达式2)如果表达式1不为空,则返回表达式1。如果表达式1为空,则返回表达式2

nullif(表达式1,表达式2)如果表达式1=表达式2,返回null ,否则返回表达式1。

case when控制结构:

有两种形式

(1)

1

<span style="font-family:FangSong_GB2312;">case 待判断值 when 值1 then 输出1when 值2 then 输出2&hellip;&hellip;when 值N then 输出Nelse 默认输出 end;  #如果输出时语句的话,最后的结尾要改成end case。输出的是值则是end同C语言的switch相比较switch(待判断值){case 值1:输出1          break;case 值2:输出2          break;&hellip;&hellip;case 值N:输出N          break;default:默认输出}</span>

Copy after login

(2)

1

<span style="font-family:FangSong_GB2312;">casewhen 判断表达式1 then 输出1when 判断表达式2 then 输出2&hellip;&hellip;when 判断表达式N then 输出Nelse 默认输出 end case; #如果输出时语句的话,最后的结尾要是end case。输出的是值则是end。</span>

Copy after login

while循环结构:

1

<span style="font-family:FangSong_GB2312;">while 判断表达式 do循环体end while;C语言中的while循环while(判断表达式){循环体;}</span>

Copy after login

loop循环结构:无条件循环

1

<span style="font-family:FangSong_GB2312;">标签:loop循环体;end loop;可以通过"leave 标签"来跳出loop循环。</span>

Copy after login

repeat循环结构:

1

<span style="font-family:FangSong_GB2312;">repeat循环体; until 判断表达式 end repeat;</span>

Copy after login

现在开始介绍存储过程,其实存储过程跟函数很像

查看当前存储过程的状态:show procedure status;

创建存储过程:

1

<span style="font-family:FangSong_GB2312;">create procedure 名称(参数列表)begin语句集end;</span>

Copy after login

参数列表总是存在的,如果没有参数则应该是空参数列表(),参数必须指定数据类型而且每个参数默认都是一个in参数。要指定为其他参数,可以在参数前面加上out或inout关键字。默认的in类似于按值传递,在存储过程中对参数进行修改,调用者是看不到的。out参数只是用来从存储过程传回数据的,无论给参数传入什么值,这个参数的初始值始终是null。对于inout参数,调用者不仅可以设置参数的初始值,而且在过程中修改参数,调用者是看得到的类似与按地址传递

删除存储过程:drop procedure 名称;

查看存储过程:show create procedure 名称/G 类似于show create table 表名 /G的作用是横向显示

调用存储过程:call 名称(参数);

声明变量:

(1)declare变量名 变量类型 默认值; 声明变量必须在开头定义,如果没有默认值,初始值为null。作用范围是在begin……end内

(2)set @变量名=初始值;定义的变量是用户变量,在存储过程之外的sql也是可以调用的

变量赋值:set 变量名=变量值 切忌直接给变量赋值(变量名= 变量值)

还有一种给一个或多个变量赋值的方法:利用“select 指定列 into 指定变量”,所以select的结果必须是单行。

示例:

所有示例,都实现将分界符设置为'$'

delimiter $

1、测试if-else控制结构

/

2、测试case……when

第一种情况:

输出是值,结尾用end。一般用于select

/

输出是语句,结尾用end case。一般用于存储过程

/

第二种情况:

输出是语句,结尾用end case。一般用于存储过程

/

输出是值,结尾用end。一般用于select

/

3、测试while循环

/

 

4、测试loop

/

 

5、测试repeat

/

 

6、带参数的存储过程

默认为in的参数:按值传递

/

初始值为0的变量tmp作为参数传入存储过程后,虽然在存储过程内对其进行修改,但调用者再次查看tmp时,值仍然为0,没有变化

out参数:

/

由第一个select可以看出,out参数不允许将实参的值传入存储过程。通过第二个和第三个select可以看出,存储过程内部修改变量后可以返回给调用者。

与按地址传递还有所不同,out只允许返回值,不允许传入值。

 

inout参数:按地址传递,形参值改变会改变实参的值

/

第一个select结果为0,说明实参的值传进存储过程。第二个和第三个select结果表明,inout可以在存储过程内部修改形参的值,从而影响实参,类似于按地址传递

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

How do you create indexes on JSON columns? How do you create indexes on JSON columns? Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

See all articles