Introduction to query commands for MySQL stored procedures

Introduction to query commands for MySQL stored procedures

As shown below:

select name from mysql.proc where db='database name';

or

select routine_name from information_schema.routines where routine_schema='database name';

or

show procedure status where db='database name';
SHOW CREATE PROCEDURE database.Stored procedure name;

Supplement: MySQL stored procedure - loop through the query result set

1. Create a stored procedure

When creating a stored procedure according to MySQL syntax, it is important to note that if you loop through the result set of the query, you need to take out the data in the result set for operation.

CREATE DEFINER=`root`@`%` PROCEDURE `alter_view_counts`()
BEGIN
 #Declare end flag DECLARE end_flag int DEFAULT 0; 
 DECLARE albumId bigint; 
 #Declare cursor album_curosr
 DECLARE album_curosr CURSOR FOR SELECT album_id FROM album; 
 #Set the termination flag DECLARE CONTINUE HANDLER FOR NOT FOUND SET end_flag=1; 
 #Open the cursor OPEN album_curosr; 
 #Traverse the cursor REPEAT
 #Get the current cursor pointer record and assign the value to the custom variable FETCH album_curosr INTO albumId;
  #Use the obtained value to perform database operations UPDATE album SET album.views_count= (SELECT SUM(light_chat.views_count) FROM `light_chat` WHERE light_chat.album_id = albumId) WHERE album.album_id = albumId;
 # Determine whether to end UNTIL end_flag END REPEAT according to end_flag; 
 #Close the cursorclose album_curosr; 
END

2. Calling a stored procedure

CALL alter_view_counts()

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Solution to the problem that MySQL commands cannot be entered in Chinese
  • Detailed explanation of encoding issues during MySQL command line operations
  • Introduction to the use of MySQL source command
  • MySQL commonly used SQL and commands from entry to deleting database and running away
  • Implementation of mysql using mysqlbinlog command to restore accidentally deleted data
  • Three methods of automatically completing commands in MySQL database
  • MySQL password contains special characters & operation of logging in from command line
  • Mysql desktop tool SQLyog resources and activation methods say goodbye to the black and white command line
  • MySQL login and exit command format
  • How to use the MySQL authorization command grant
  • Summary of MySQL basic common commands

<<:  Introduction to CSS foreground and background automatic color matching technology (demo)

>>:  About the VMware vcenter unauthorized arbitrary file upload vulnerability (CVE-2021-21972)

Recommend

Detailed graphic and text instructions for installing MySQL 5.7.20 on Mac OS

Installing MySQL 5.7 from TAR.GZ on Mac OS X Comp...

MySQL uses UNIQUE to implement non-duplicate data insertion

SQL UNIQUE constraint The UNIQUE constraint uniqu...

Several common methods for passing additional parameters when submitting a form

When submitting a form, you may encounter situatio...

Avoid abusing this to read data in data in Vue

Table of contents Preface 1. The process of using...

How to modify the contents of an existing Docker container

1. Docker ps lists containers 2. Docker cp copies...

Detailed explanation of object literals in JS

Table of contents Preface 1. Set the prototype on...

Detailed explanation of Docker basic network configuration

External Access Randomly map ports Using the -P f...

Learn MySQL database in one hour (Zhang Guo)

Table of contents 1. Database Overview 1.1 Develo...

Vue sample code for online preview of office files

I'm working on electronic archives recently, ...

VUE introduces the implementation of using G2 charts

Table of contents About G2 Chart use Complete cod...

How to Change Colors and Themes in Vim on Linux

Vim is a text editor that we use very often in Li...

A brief discussion on React Component life cycle functions

What are the lifecycle functions of React compone...