Home Database Mysql Tutorial Mysql 存储过程_MySQL

Mysql 存储过程_MySQL

Jun 01, 2016 pm 01:51 PM
storage

转换结束符:

delimiter /

存储过程:

 

create procedure test 

()#或者是(a int,b int,out b int) 

begin 

#这里写你的操作 

end / 

 

调用:call test()/

存储函数:

 

create function test 

()#或者是(a int,b int) 

returns int #函数体必须包含一个RETURN value语句 

begin 

#这里写你的操作 

return 1; 

end/

 

调用:select test()/

备注:存储函数内不能查表(不能使用select 语句)储过程可以

语法:

  1. 存储过程中的局部变量前面无@符号用declare定义,且只能在begin end块中,全局变量为@var,前面带有@符号

     

    create procedure test(out a int) 

    begin 

    declare ab int default 1; 

    set a=100; 

    set a=ab; 

    end 

     

    declare 定义的变量的优先级最高,所有当out a 等变量和局部变量相同名的时候,该变量只在该begin块中有效

    SELECT id,data INTO x,y FROM test.t1 通过查询赋值变量

    begin不能并列使用

2.返回值通过OUT参数得到

3.判断

if条件 then

操作

end if;

case 值 (可选)

when 条件 then 操作

when 条件 then 操作

end

4.循环

while 条件 do … end while

loop … end loop

repeat … end repeat

goto

while … end while 例

 

CREATE PROCEDURE p14 () 

BEGIN 

DECLARE v INT; 

SET v = 0; 

WHILE v 
INSERT INTO t VALUES (v); 

SET v = v + 1; 

END WHILE; 

END; // 

 

 

labels 使用

lab1 :begin

操作

end lab1;

 

lab2:while 条件 do

#操作

#可以使用leave lab2;跳出循环

#可以使用iterate lab2;跳过当次循环

end while lab2

 

GOTO 使用

 

b1:begin 

declare i int; 

set i=1; 

 

label lab1; 

select 'hi'; 

set i=i+1; 

if(i
goto lab1; 



end b1;   

     

5.错误处理

declare continue handler for sqlstate 'error number' 操作 end;

declare exit handler for sqlstate 'error number' 操作 end;

sqlstate 'error number' 还可以是

not found #空行

sqlexception #发生错误

sqlwarning #发生警告

以上语句均在发送错误的时候才触发

6.光标

只读的及不滚动, 声明处理程序之前被声明, SELECT语句不能有INTO子句。

例:

 

CREATE PROCEDURE curdemo() 

BEGIN 

declare a,b,done int; 

DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1; 

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;#声明错误处理 

OPEN cur1;#打开光标 

while done!=1 do 

fetch cur1 into a,b; 

if a>b then select 'a'; 

else select 'b'; 

end if ; 

end while; 

close cur1; 

end ; 

 

 

备注:取得最后插入的ID 函数为last_insert_id();所有的存储过程等信息都在INFORMATION_SCHEMA库中

 

 

查询存储过程

 

show create procedure test/ #查询存储过程详细 

show create function test/ 

 

 

触发器: TRIGGER

类型:

INSERT:将新行插入表时激活触发程序,例如,通过INSERT、LOAD DATA和REPLACE语句。

UPDATE:更改某一行时激活触发程序,例如,通过UPDATE语句。

DELETE:从表中删除某一行时激活触发程序,例如,通过DELETE和REPLACE语句。

使用OLD.col_name来引用更新前的某一行的列,也能使用NEW.col_name来引用更新后的行中的列

时间: BEFORE AFTER

例:

 

CREATE TRIGGER testtrio BEFORE INSERT ON test1 

  FOR EACH ROW BEGIN 

   #操作 

  END

 

 

动态SQL:

语法:

PREPARE stmt_name FROM preparable_stmt;
EXECUTE stmt_name [USING @var_name [, @var_name] ...];
{DEALLOCATE | DROP} PREPARE stmt_name;

实例:

 

mysql> SET @a=1;
mysql> PREPARE STMT FROM "SELECT * FROM tbl LIMIT ?"; 
mysql> EXECUTE STMT USING @a; 
mysql> SET @skip=1; SET @numrows=5; 
mysql> PREPARE STMT FROM "SELECT * FROM tbl LIMIT ?, ?"; 
mysql> EXECUTE STMT USING @skip, @numrows;

 

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 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)

Huawei will launch innovative MED storage products next year: rack capacity exceeds 10 PB and power consumption is less than 2 kW Huawei will launch innovative MED storage products next year: rack capacity exceeds 10 PB and power consumption is less than 2 kW Mar 07, 2024 pm 10:43 PM

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

Vue3+TS+Vite development skills: how to encrypt and store data Vue3+TS+Vite development skills: how to encrypt and store data Sep 10, 2023 pm 04:51 PM

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

How to clear cache on Windows 11: Detailed tutorial with pictures How to clear cache on Windows 11: Detailed tutorial with pictures Apr 24, 2023 pm 09:37 PM

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

Git installation process on Ubuntu Git installation process on Ubuntu Mar 20, 2024 pm 04:51 PM

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

How to correctly use sessionStorage to protect sensitive data How to correctly use sessionStorage to protect sensitive data Jan 13, 2024 am 11:54 AM

How to correctly use sessionStorage to store sensitive information requires specific code examples. Whether in web development or mobile application development, we often need to store and process sensitive information, such as user login credentials, ID numbers, etc. In front-end development, using sessionStorage is a common storage solution. However, since sessionStorage is browser-based storage, some security issues need to be paid attention to to ensure that the stored sensitive information is not maliciously accessed and used.

Full analysis of Java collection framework: dissecting data structure and revealing the secret of efficient storage Full analysis of Java collection framework: dissecting data structure and revealing the secret of efficient storage Feb 23, 2024 am 10:49 AM

Overview of Java Collection Framework The Java collection framework is an important part of the Java programming language. It provides a series of container class libraries that can store and manage data. These container class libraries have different data structures to meet the data storage and processing needs in different scenarios. The advantage of the collection framework is that it provides a unified interface, allowing developers to operate different container class libraries in the same way, thereby reducing the difficulty of development. Data structures of the Java collection framework The Java collection framework contains a variety of data structures, each of which has its own unique characteristics and applicable scenarios. The following are several common Java collection framework data structures: 1. List: List is an ordered collection that allows elements to be repeated. Li

How do PHP and swoole achieve efficient data caching and storage? How do PHP and swoole achieve efficient data caching and storage? Jul 23, 2023 pm 04:03 PM

How do PHP and swoole achieve efficient data caching and storage? Overview: In web application development, data caching and storage are a very important part. PHP and swoole provide an efficient method to cache and store data. This article will introduce how to use PHP and swoole to achieve efficient data caching and storage, and give corresponding code examples. 1. Introduction to swoole: swoole is a high-performance asynchronous network communication engine developed for PHP language. It can

Methods and techniques for data caching and storage using PHP arrays Methods and techniques for data caching and storage using PHP arrays Jul 16, 2023 pm 02:33 PM

Methods and techniques for using PHP arrays to implement data caching and storage. With the development of the Internet and the rapid growth of data volume, data caching and storage have become one of the issues that we must consider during the development process. As a widely used programming language, PHP also provides a wealth of methods and techniques to implement data caching and storage. Among them, using PHP arrays for data caching and storage is a simple and efficient method. 1. Data caching The purpose of data caching is to reduce the number of accesses to the database or other external data sources, thereby improving

See all articles