This article uses examples to explain the principles and usage of MySQL stored procedures. Share with you for your reference, the details are as follows: A stored procedure contains a series of executable SQL statements. The stored procedure is stored in MySQL and can be executed by calling its name. Advantages of stored procedures
Disadvantages of stored procedures
Stored procedure without parameters delimiter // create procedure p1() BEGIN select * from blog; INSERT into blog(name,sub_time) values("xxx",now()); END // delimiter ; #Call p1() in mysql #Call cursor.callproc('p1') based on pymysql in python print(cursor.fetchall()) Stored procedures with parameters For stored procedures, you can receive parameters, which are divided into three categories:
Stored procedure with in mysql> select * from emp; +----+----------+-----+--------+ | id | name | age | dep_id | +----+----------+-----+--------+ | 1 | zhangsan | 18 | 1 | | 2 | lisi | 19 | 1 | | 3 | egon | 20 | 2 | | 5 | alex | 18 | 2 | +----+----------+-----+--------+ 4 rows in set (0.30 sec) mysql> delimiter // mysql> create procedure p2(in n1 int, in n2 int) -> begin -> select * from emp where id >n1 and id <n2; -> end // Query OK, 0 rows affected (0.28 sec) mysql> delimiter; mysql> call p2(1,3) -> ; +----+------+-----+--------+ | id | name | age | dep_id | +----+------+-----+--------+ | 2 | lisi | 19 | 1 | +----+------+-----+--------+ 1 row in set (0.07 sec) Query OK, 0 rows affected (0.07 sec) #Call cursor.callproc('p2',(1,3)) based on pymysql in python print(cursor.fetchall()) With out mysql> delimiter // mysql> create procedure p3( in n1 int, out res int) -> begin -> select * from emp where id >n1; ->set res=1; -> end // Query OK, 0 rows affected (0.28 sec) mysql> delimiter; mysql> set @res=0; Query OK, 0 rows affected (0.00 sec) mysql> call p3(3,@res); +----+------+-----+--------+ | id | name | age | dep_id | +----+------+-----+--------+ | 5 | alex | 18 | 2 | +----+------+-----+--------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.01 sec) mysql> select @res; +------+ | @res | +------+ | 1 | +------+ 1 row in set (0.00 sec) #Call cursor.callproc('p3',(3,0)) based on pymysql in python #0 is equivalent to set @res=0 print(cursor.fetchall()) #Query the query results of select cursor.execute('select @_p3_0,@_p3_1;') #@p3_0 represents the first parameter, @p3_1 represents the second parameter, that is, the return value print(cursor.fetchall()) Example with inout delimiter // create procedure p4( inout n1 int ) BEGIN select * from blog where id > n1; set n1 = 1; END // delimiter ; #Call set @x=3 in mysql; call p4(@x); select @x; #Call cursor.callproc('p4',(3,)) based on pymysql in python print(cursor.fetchall()) #Query the query results of select cursor.execute('select @_p4_0;') print(cursor.fetchall()) Transactions #Introduction delimiter // create procedure p4( out status int ) BEGIN 1. Declare that if an exception occurs, execute { set status = 1; rollback; } Start transaction -- subtract 100 from Qin Bing's account -- Add 90 to Fang Shaowei's account -- Zhang Gen's account plus 10 commit; end set status = 2; END // delimiter ; #Implement delimiter // create PROCEDURE p5( OUT p_return_code tinyint ) BEGIN DECLARE exit handler for sqlexception BEGIN -- ERROR set p_return_code = 1; rollback; END; DECLARE exit handler for sqlwarning BEGIN --WARNING set p_return_code = 2; rollback; END; START TRANSACTION; DELETE from tb1; #Execution failed insert into blog(name,sub_time) values('yyy',now()); COMMIT; --SUCCESS set p_return_code = 0; #0 represents successful execution END // delimiter ; #Call the stored procedure in mysql set @res=123; call p5(@res); select @res; #Calling stored procedures based on pymysql in python cursor.callproc('p5',(123,)) print(cursor.fetchall()) #Query the query results of select cursor.execute('select @_p5_0;') print(cursor.fetchall()) Execution of stored procedures Execute in mysql -- call proc_name() without parameters -- With parameters, all in call proc_name(1,2) -- There are parameters, in, out, inout set @t1=0; set @t2=3; call proc_name(1,2,@t1,@t2) Execute in pymsql #!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # Execute the stored procedure cursor.callproc('p1', args=(1, 22, 3, 4)) # Get the parameters after execution and storage cursor.execute("select @_p1_0,@_p1_1,@_p1_2,@_p1_3") result = cursor.fetchall() conn.commit() cursor.close() conn.close() print(result) Deleting a stored procedure drop procedure proc_name; Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL stored procedure skills", "MySQL common function summary", "MySQL log operation skills", "MySQL transaction operation skills summary" and "MySQL database lock related skills summary" I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
>>: Vue routing relative path jump method
Preface Every good habit is a treasure. This arti...
The nginx configuration is as follows: Such as ht...
Absolute length px px is the pixel value, which i...
From today on, I will regularly organize some smal...
Why use prettier? In large companies, front-end d...
Preface This article mainly introduces the analys...
Preface In the previous article Detailed Explanat...
Table of contents Overview 1. Function debounce 2...
The operating environment of this tutorial: Windo...
This article example shares the specific code of ...
Table of contents Vue3 encapsulation message prom...
A docker container state transition diagram Secon...
The previous article introduced two methods to ch...
0. Environment Operating system for this article:...
Table of contents 1. Union Type 2. Crossover Type...