Example analysis of mysql stored procedure usage

Example analysis of mysql stored procedure usage

This article describes the usage of MySQL stored procedures. Share with you for your reference, the details are as follows:

Overview:

Simply put, it is a set of SQL statements that are powerful and can implement some relatively complex logical functions, similar to the methods in the JAVA language;

A stored procedure is similar to a trigger in that both are a set of SQL statements, but a stored procedure is called actively and is more powerful than a trigger, which is called automatically when something happens.

Example

DELIMITER //
CREATE PROCEDURE proc (IN num INT)
BEGIN
SELECT * FROM v9_qd_account limit num;
END //
DELIMITER ;

implement:

SET @p_in=5;
CALL proc(@p_in);

or

CALL proc(5);

cycle

DECLARE num int default 5;
SET num = 1;
SET num = num + 1;

DELIMITER //
CREATE PROCEDURE proc(IN sname VARCHAR(20), IN pwd VARCHAR(5), IN qd INT, IN start INT, IN end INT)
  begin
  DECLARE var INT;
  DECLARE myname VARCHAR(30);
  SET var=start;
  while var<end do
    SET myname = CONCAT(sname,LPAD(var,3,'0'));
    insert into v9_qd_account (storename,password,qudao,regdate) value(myname,md5(pwd),qd,UNIX_TIMESTAMP());
    SET var=var+1;
  end while;
  end //
DELIMITER ;

Call

CALL proc('test','123456',1,1,21);

Check

SHOW PROCEDURE STATUS LIKE 'C%' \G; View stored procedures starting with the letter C SHOW CREATE PROCEDURE proc \G; View the status and creation statement View in information_schema.Routines

delete

DROP PROCEDURE IF EXISTS proc;
DROP FUNCTION mytest;

Tips

If there is Chinese in the parameter, you can do this

CREATE PROCEDURE useInfo(IN u_name VARCHAR(50) character set gbk,OUT u_age INT) ......

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:
  • Definition and assignment of variables in mysql stored procedures
  • Detailed explanation of mysql stored procedure
  • Introduction to the use of MySQL stored procedure cursor loop
  • MySQL stored procedure example (including transactions, output parameters, nested calls)
  • Detailed explanation of MySql stored procedures and functions
  • mysql query database stored procedures and functions statement
  • MySQL stored procedure example with input and output parameters
  • Example analysis of stored procedures in MySQL and how to call stored procedures
  • The difference between MySQL stored procedures and functions
  • How to execute dynamic SQL statements in MySQL stored procedures
  • Example of using cursor in mysql stored procedure
  • Mysql stored procedure nested loop using cursor sample code

<<:  How to deploy services in Windows Server 2016 (Graphic Tutorial)

>>:  Detailed explanation of nodejs built-in modules

Recommend

Awk command line or script that helps you sort text files (recommended)

Awk is a powerful tool that can perform some task...

How to import/save/load/delete images locally in Docker

1. Docker imports local images Sometimes we copy ...

ie filter collection

IE gave us a headache in the early stages of deve...

Detailed explanation of the process of installing MySQL on Ubuntu 18.04.4

Let's take a look at the process of installin...

Ways to improve MongoDB performance

MongoDB is a high-performance database, but in th...

Detailed explanation of viewing and setting file permissions on Mac

Preface To modify file permissions in the termina...

JS+Canvas realizes dynamic clock effect

A dynamic clock demo based on Canvas is provided ...

Complete steps to use vue-router in vue3

Preface Managing routing is an essential feature ...

Token verification login in Vue project (front-end part)

This article example shares the specific code of ...

Html sample code for reading and displaying pictures in a local folder

One purpose Select a local folder on the Html pag...

Ubuntu installation cuda10.1 driver implementation steps

1. Download cuda10.1: NVIDIA official website lin...