mysql-存储过程
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可以在存储过程内部修改形参的值,从而影响实参,类似于按地址传递

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



This website reported on March 7 that Dr. Zhou Yuefeng, President of Huawei's Data Storage Product Line, recently attended the MWC2024 conference and specifically demonstrated the new generation OceanStorArctic magnetoelectric storage solution designed for warm data (WarmData) and cold data (ColdData). Zhou Yuefeng, President of Huawei's data storage product line, released a series of innovative solutions. Image source: Huawei's official press release attached to this site is as follows: The cost of this solution is 20% lower than that of magnetic tape, and its power consumption is 90% lower than that of hard disks. According to foreign technology media blocksandfiles, a Huawei spokesperson also revealed information about the magnetoelectric storage solution: Huawei's magnetoelectronic disk (MED) is a major innovation in magnetic storage media. First generation ME

TRedis caching technology is a high-performance memory caching technology that can improve the performance and response speed of a website or application. In this article, we will introduce the basic concepts of TRedis caching technology and how to use it in your applications. What is TRedis caching technology? TRedis is a memory caching technology that stores frequently used data in memory, thereby increasing the speed of accessing this data. The main idea of this technique is to reduce the load on the database or disk by using in-memory caching

Vue3+TS+Vite development tips: How to encrypt and store data. With the rapid development of Internet technology, data security and privacy protection are becoming more and more important. In the Vue3+TS+Vite development environment, how to encrypt and store data is a problem that every developer needs to face. This article will introduce some common data encryption and storage techniques to help developers improve application security and user experience. 1. Data Encryption Front-end Data Encryption Front-end encryption is an important part of protecting data security. Commonly used

ECache is a Java caching framework that provides a simple yet powerful way to reduce the response time of computer applications. It enables applications to respond to client requests faster and improves system throughput by storing data in memory. In this article, we will introduce some basic knowledge of ECache caching technology, including its advantages, installation and usage, etc. 1. Advantages of ECache Improve system performance: ECache stores cache data in memory, which means that applications

What is cache? A cache (pronounced ka·shay) is a specialized, high-speed hardware or software component used to store frequently requested data and instructions, which in turn can be used to load websites, applications, services, and other aspects of the system faster part. Caching makes the most frequently accessed data readily available. Cache files are not the same as cache memory. Cache files refer to frequently needed files such as PNGs, icons, logos, shaders, etc., which may be required by multiple programs. These files are stored in your physical drive space and are usually hidden. Cache memory, on the other hand, is a type of memory that is faster than main memory and/or RAM. It greatly reduces data access time since it is closer to the CPU and faster compared to RAM

JSP file opening method JSP (JavaServerPages) is a dynamic web page technology that allows programmers to embed Java code in HTML pages. JSP files are text files that contain HTML code, XML tags, and Java code. When a JSP file is requested, it is compiled into a JavaServlet and then executed by the web server. Methods of Opening JSP Files There are several ways to open JSP files. The easiest way is to use a text editor,

Redisson is a Redis-based caching solution for Java applications. It provides many useful features that make using Redis as a cache in Java applications more convenient and efficient. The caching functions provided by Redisson include: 1. Distributed mapping (Map): Redisson provides some APIs for creating distributed maps. These maps can contain key-value pairs, hash entries, or objects, and they can support sharing among multiple nodes.

Git is a fast, reliable, and adaptable distributed version control system. It is designed to support distributed, non-linear workflows, making it ideal for software development teams of all sizes. Each Git working directory is an independent repository with a complete history of all changes and the ability to track versions even without network access or a central server. GitHub is a Git repository hosted on the cloud that provides all the features of distributed revision control. GitHub is a Git repository hosted on the cloud. Unlike Git which is a CLI tool, GitHub has a web-based graphical user interface. It is used for version control, which involves collaborating with other developers and tracking changes to scripts and
