search
HomeDatabaseMysql Tutorialmysql-存储过程

mysql---存储过程 了解存储过程之前,先了解一下mysql的控制结构。 类C语言(if……else、while循环等)SQL也有自己的控制结构。 if……else控制结构: 例如: (1) span style=font-family:FangSong_GB2312;if 判断表达式 then 执行语句;end if;与c语言进

mysql---存储过程

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

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


if……else控制结构:

例如:

(1)

<span style="font-family:FangSong_GB2312;">if 判断表达式 
   then 执行语句;
end if;

与c语言进行比较
if(判断表达式)
   执行语句;</span>

(2)

<span style="font-family:FangSong_GB2312;">if 判断表达式1 
   then 执行语句1;
else
   then  执行语句2;
end if;

与c语言进行比较
if(判断表达式1)
   执行语句1;
else 
   执行语句2;</span>

(3)

<span style="font-family:FangSong_GB2312;">if 判断表达式1 
   then 执行语句1;
elseif 判断表达式2 
   then  执行语句2;
……
elseif 判断表达式N 
   then 执行语句N;
else
   执行语句N+1;
end if;

与c语言进行比较
if(判断表达式1)
   执行语句1;
else if(判断表达式2)
   执行语句2;
……
else if(判断表达式N)
   执行语句N;
else
   执行语句N+1;</span>

需要注意所有的执行语句和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)

<span style="font-family:FangSong_GB2312;">case 待判断值 
when 值1 then 输出1
when 值2 then 输出2
……
when 值N then 输出N
else 默认输出 end;  #如果输出时语句的话,最后的结尾要改成end case。输出的是值则是end

同C语言的switch相比较
switch(待判断值){
case 值1:输出1
          break;
case 值2:输出2
          break;
……
case 值N:输出N
          break;
default:默认输出
}</span>

(2)

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

while循环结构:

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

C语言中的while循环
while(判断表达式){
循环体;
}</span>

loop循环结构:无条件循环

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

repeat循环结构:

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


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

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

创建存储过程:

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

参数列表总是存在的,如果没有参数则应该是空参数列表(),参数必须指定数据类型而且每个参数默认都是一个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
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
MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL: Not a Programming Language, But...MySQL: Not a Programming Language, But...Apr 13, 2025 am 12:03 AM

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL: An Introduction to the World's Most Popular DatabaseMySQL: An Introduction to the World's Most Popular DatabaseApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

The Importance of MySQL: Data Storage and ManagementThe Importance of MySQL: Data Storage and ManagementApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

Why Use MySQL? Benefits and AdvantagesWhy Use MySQL? Benefits and AdvantagesApr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Apr 12, 2025 am 12:16 AM

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use