当前位置:网站首页>MySQL stored procedure
MySQL stored procedure
2022-06-11 04:16:00 【Prince you】
One 、 Introduction to stored procedures
A stored procedure is a set of functions to accomplish SQL Statement set .MySQL 5.0 Finally started to support stored procedures , It is the most important function in the database , Use common or complex work in advance SQL The statement is written and stored with a specified name , This process is compiled and optimized and stored in the database server , So it's called a stored procedure .
Why use stored procedures :
- encapsulation : After the stored procedure is created , Can be called multiple times in a program , Without having to rewrite the stored procedure's SQL sentence , And database professionals can modify stored procedures at any time , It doesn't affect the application source code that calls it .
- Can be enhanced SQL The function and flexibility of statements : Stored procedures can be written with process control statements , Very flexible , It can complete complex judgment and more complex operation .
- Can reduce network traffic : Because stored procedures run on the server side , And fast execution , So when the stored procedure is called on the client computer , All that's passed in the network is the call statement , This reduces network load .
- High performance : After the stored procedure is executed once , The generated binary code resides in the buffer , In later calls , Just execute the binary code from the buffer , So it improves the efficiency and performance of the system .
- Improve database security and data integrity : Using stored procedures can complete all database operations , And can control the access right of database information by programming .
Two 、MySQL Create stored procedure
Syntax and parameters
grammar :
CREATE PROCEDURE Process name ([proc_parameter[,...]])
[characteristic ...] routine_body
explain
proc_parameter : Express [ IN | OUT | INOUT ] param_name type
- IN Input parameters : Indicates that the caller passes a value... To the procedure
- OUT Output parameters : Indicates that the procedure passes out a value to the caller
- INOUT Input/output parameter : It means that the caller passes in a value to the procedure , It also indicates that the procedure passes out a value to the caller
type : Indicates any valid MySQL data type
characteristic : Express
LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'
routine_body Means effective SQL Procedure statement or statement finger :“ The process of body ”- NOT DETERMINISTIC : If a program or thread always produces the same result for the same input parameter , It is considered to be “ affirmatory ”, Otherwise, it would be “ Not sure ” Of . If neither is given DETERMINISTIC There is no given NOT DETERMINISTIC, The default is NOT DETERMINISTIC.
- CONTAINS SQL : A statement indicating that a subroutine does not contain read or write data , The default is CONTAINS SQL
- NO SQL Indicates that the subroutine does not contain SQL sentence
- READS SQL DATA Indicates that the subroutine contains statements that read data , But it doesn't include statements that write data
- MODIFIES SQL DATA Indicates that the subroutine contains statements that write data
- SQL SECURITY Features can be used to specify The subroutine should be executed with the permission of the creator of the subroutine , Or use the caller's permission to execute . The default value is DEFINER. stay SQL:2003 The middle is a new feature . The creator or caller must be accessed by License of the database associated with the subroutine . stay MySQL 5.1 in , There has to be EXECUTE Permission to execute subroutines . The user who must have this permission is either the definer , Or the caller , It depends. SQL SECURITY How the feature is set .
To be in MySQL 5.1 Create a stored program in , Must possess CREATE ROUTINE jurisdiction , also ALTER ROUTINE and EXECUTE Permission is automatically granted to its Creator .
By default , Subroutines are associated with the current database . To explicitly associate a subroutine with a given database , You can specify the name of the subroutine when you create it db_name.sp_name.
in、out、inout Use
1、 Parameterless stored procedure
/* Create a parameterless stored procedure */
DELIMITER $$
CREATE PROCEDURE p_student() # Declaring stored procedures
BEGIN # Stored procedure start symbol
SELECT * FROM student;
END$$ # Stored procedure end symbol
DELIMITER $$ # Declaration statement Terminator You can also use DELIMITER // 
# Calling stored procedure
call p_student 
2、in Parameter stored procedures
/*in Stored procedure for input parameters Input id The line of the query */
DELIMITER $$
CREATE PROCEDURE p_student2(IN stu_num INT)
BEGIN
SELECT * FROM student WHERE num = stu_num;
END$$
DELIMITER $$# Calling stored procedure
call p_student2(2);
3、out Parameter stored procedure description
/*out Stored procedure */
DELIMITER $$
CREATE PROCEDURE p_student3(OUT stu_name VARCHAR(20))
BEGIN
SELECT NAME INTO stu_name FROM student WHERE num=2;
END$$
DELIMITER $$# Calling stored procedure Store the result of execution in @name in
call p_student3(@name);
# Inquire about @name
select @name
4、inout Parameter stored procedure description
/*inout Stored procedure */
DELIMITER$$
CREATE PROCEDURE p_student4(INOUT stu_name VARCHAR(20))
BEGIN
SELECT NAME INTO stu_name FROM student WHERE name=stu_name;
END$$
DELIMITER $$# Set up @name Value
set @name=' Zhang San ';
# take @name Pass in and return the result
call p_student4(@name);
# The result returned by the query
select @name
边栏推荐
- 邪恶的CSRF
- [pan micro E9 development] single sign on Kingdee eas
- Market prospect analysis and Research Report of surround packing machine in 2022
- Guanghetong 5g module shines brightly and has won the "2021 science and technology award of China Electronics Society"
- Market prospect analysis and Research Report of digital line scan camera in 2022
- Market prospect analysis and Research Report of denitrification unit in 2022
- Embedded basic interface-i2s
- 给你一个项目,你将如何开展性能测试工作?
- AI助力,释放法务势能!iTerms合同智审系统重磅发布
- 6. form label
猜你喜欢

Talk about data center network again

Seven easy-to-use decorators

众昂矿业:氟化工是萤石的主要消耗领域

A - Eddy's AC puzzle (C language)

Vulkan-官方示例解读-RayTracing

数据类型的转换和条件控制语句

【网络篇】套接字编程

It's 2022. When will the "module freedom" be realized?

Esp32 porting lvgl

Composition and configuration of GPS Beidou clock synchronization (satellite time synchronization system) in power plant
随机推荐
SSLStrip 终极版 —— location 劫持
Source insight 4.0 setting shortcut keys for comments and uncomments
Market prospect analysis and Research Report of beam combiner in 2022
域名解析耗时是什么?域名解析耗时影响因素有哪些?
Eth Transfer
Discussion on the development trend of remote power management unit (Intelligent PDU)
golang泛型:generics
再聊数据中心网络
GPS Beidou time service, NTP makes network clock synchronization more accurate
MySQL锁总结
Feature selection algorithm based on bare bones particleswarm optimization
使用工具类按一定规则读取Excel文件
QT日志模块的个性化使用
Unity prefab scene 冲突合并工具 UnityYAMLMerge
Embedded basic interface-i2s
Market prospect analysis and Research Report of programmable digital temperature regulator in 2022
邪恶的CSRF
ESP series module burning firmware
Linq. pdf
三层带防护内网红队靶场