DAY 63 Advanced statement of mysql: stored procedure

 What is a stored procedure

A stored procedure is a set of SQL statements to complete a specific function.

In the process of using the stored procedure, common or complex work is written in advance using SQL statements and stored with a specified name. This process is compiled and optimized and stored in the database server. When you need to use the stored procedure, you only need to call it. Stored procedures are faster and more efficient than traditional SQL in execution

Advantages of stored procedures:

1. After one execution, the generated binary code will reside in the buffer to improve execution efficiency.

2, SQL statement plus the collection of control statements, high flexibility.

3. Store on the server side and reduce the network load when the client calls.

4. It can be called repeatedly and can be modified at any time without affecting the client call.

5. Can complete all database operations, and can also control the information access rights of the database

Create, call, and view stored procedures

 ##创建存储过程:
 DELIMITER $$                  #将语句的结束符号从分号;临时改为两个$$ (可以是自定义)
 CREATE PROCEDURE Proc ()      #创建存储过程,过程名为Proc,不带参数
 -> BEGIN                      #过程体以关键字BEGIN开始
 -> select * from store_info;  #过程体语句
 -> END $$                     #过程体以关键字END结束
 ​
 DELIMITER;                    #将语句的结束符号恢复为分号
 ​
 ##调用存储过程##
 CALL Proc;
 ​
 ##查看存储过程##
 SHOW CREATE PROCEDURE [数据库.]存储过程名;  #查看某个存储过程的具体信息
 ​
 SHOW CREATE PROCEDURE Proc;
 SHOW CREATE PROCEDURE Proc\G
 ​
 SHOW PROCEDURE STATUS [LIKE '%Proc%'] \G
 ​
 ##删除存储过程##
 #存储过程内容的修改方法是通过删除原有存储过程,之后再以相同的名称创建新的存储过程。
 DROP PROCEDURE IF EXISTS Proc;
 #仅当存在时删除,不添加If EXISTS 时,如果指定的过程不存在,则产生一个错误。
 

Create and call stored procedures

delimiter $$
 create procedure proc01 ()    #创建存储过程,过程名为proc01,不带参数
 -> begin                      #过程体以关键字BEGIN开始
 -> create table student(id int,name char(10),age int);
 -> insert into student values(1,'zhangsan',18);
 -> insert into student values(2,'lisi',18);
 -> select * from student;
 -> end $$                     #过程体以关键字END结束
 ​
 delimiter ;                   #将语句的结束符号恢复为分号
 ​
 call proc01;                  #调用存储过程


View stored procedures

 show create procedure proc01;
 show create procedure proc01\G       #查看存储过程的具体信息
 ​
 show procedure status like '%Proc01%'\G

delete stored procedure

The method of modifying the content of the stored procedure is by deleting the original stored procedure and then creating a new stored procedure with the same name

 drop procedure if exists proc01;
 #仅当存在时删除,不添加If EXISTS 时,如果指定的过程不存在,则产生一个错误。

 stored procedure parameters

  • IN input parameter:  Indicates that the caller passes in a value to the procedure. (Incoming values ​​can be literals or variables)
  • OUT output parameter:  Indicates that the procedure transmits a value to the caller. (Multiple values ​​can be returned) (Outgoing values ​​can only be variables)
  • INOUT input and output parameters:  not only means that the caller passes in a value to the procedure, but also means that the procedure sends a value to the caller. (The value can only be a variable) Note: The variable name cannot contain underscores.

Note: Variable names cannot contain underscores.

 delimiter $$
 create procedure proc2(in stuname char(20))    #参数为stuname,数据类型一定要与下面的where语句后字段的数据类型相同
 -> begin
 -> select * from student where name = stuname;
 -> end $$
 delimiter ;
  
 call proc2('zhangsan');      #调用存储过程,并传入参数'zhangsan'

 

 Control Statements for Stored Procedures

prepare a table first

 create table t (id int(10)) ;
 insert into t values (10) ;

Conditional statement if-then-else . . . . end if 

 delimiter $$  
 create procedure proc03(in innum int)   #创建存储过程proc03,参数为innum,类型为int 
 -> begin 
 -> declare var int;         #定义变量var为int类型 
 -> set var=innum*2;         #变量var的值等于传入的参数值乘2
 -> if var>=10 then          #当var的值大于10时,id值会加1,否则减1
 -> update t set id=id+1;  
 -> else 
 -> update t set id=id-1;  
 -> end if;  
 -> end $$
  
 delimiter ;
 ​
 call proc03(8);    #调用存储过程,并传入参数8
 ​
 call proc03(3);    #调用存储过程,并传入参数3

Loop statement while ···· end while

 delimiter $$                   #修改默认结束符为$$
 create procedure proc04()      #创建存储过程proc04
 -> begin                       #过程体以关键字begin开始
 -> declare var int(10);        #定义变量var为int类型
 -> set var=0;                  #var的起始值为0
 -> while var<6 do              #使用while循环,当var值小于6时满足条件,则向表中插入var的值
 -> insert into t values(var);  
 -> set var=var+1;              #每次循环后var值自增1
 -> end while;                  #结束while循环
 -> end $$                      #创建存储过程结束
 ​
 delimiter ;                    #重新修改默认结束符为原始的;
 ​
 call proc04;                   #调用存储过程proc04

The declare command can also be used in the shell, for example:

decalre -i var Declare the variable var as an integer (that is, add an integer attribute).

decalre +i var Delete the integer attribute of the variable var

Guess you like

Origin blog.csdn.net/weixin_57560240/article/details/130795139