This article uses examples to describe how to create (CREATE PROCEDURE) and call (CALL) MySQL stored procedures and how to create (DECLARE) and assign (SET) variables. Share with you for your reference, the details are as follows: Stored procedure creation (CREATE PROCEDURE) and call (CALL) We won’t talk about any definitions or terms here, just look at the examples and slowly get familiar with it. Now let's create a simple stored procedure called GetAllProducts(). This GetAllProducts() stored procedure is mainly used to select all products from the products table. Let's start the mysql client tool and type the following command: DELIMITER // CREATE PROCEDURE GetAllProducts() BEGIN SELECT * FROM products; END // DELIMITER ; Let's take a closer look at the above SQL:
Through the above SQL, we have created a stored procedure. When we are done, we will call the stored procedure. Let's take a look at the calling syntax: CALL STORED_PROCEDURE_NAME(); Let's take a look at the specific SQL that calls the GetAllProducts() stored procedure: CALL GetAllProducts(); Run the above SQL and you will see the results of running the SQL in the stored procedure. Variable creation (DECLARE) and assignment (SET) As we all know, a variable is a named data object whose value can be changed during the execution of a stored procedure. Next, we will try to use variables in the stored procedure to save the direct/indirect results. These variables are local variables of the stored procedure, but we must note that the variable must be declared before it can be used. If we want to declare a variable in a stored procedure, we can use the DECLARE statement. Let's take a look at the SQL syntax: DECLARE variable_name datatype(size) DEFAULT default_value; Let's take a look at what the above sql means:
Next, we will declare a variable called total_sale, with a data type of INT and a default value of 0. Let's look at the sql: DECLARE total_sale INT DEFAULT 0; Among them, MySQL allows the use of a single DECLARE statement to declare two or more variables that share the same data type. Let's take a look at the sql: DECLARE x, y INT DEFAULT 0; In the above sql, we declared two integer variables x and y and set their default values to 0. Now that the variable has been set, it is time to assign a value. If we want to assign a value to a variable, we can use the SET statement. Let's take a look at an example: DECLARE total_count INT DEFAULT 0; SET total_count = 10; In the above sql statement, we assign the value 10 to the total_count variable. In addition to the SET statement, you can also use the SELECT INTO statement to assign the result of a query to a variable. Let's look at an example: DECLARE total_products INT DEFAULT 0 SELECT COUNT(*) INTO total_products FROM products In the above sql, we should have the following understanding:
We know that a variable has its own scope, which is used to define its life cycle. But if you declare a variable inside a stored procedure, it will go out of scope when the stored procedure's END statement is reached, and therefore cannot be accessed in other code blocks. We can understand that if we declare a variable inside a BEGIN END block, then it will go out of scope if END is reached. We can also declare two or more variables with the same name in different scopes because a variable is valid only within its own scope. However, declaring variables with the same name in different scopes is not a good programming practice. Among them, variables starting with the @ symbol are session variables, which are available and accessible until the session ends. 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:
|
<<: Docker exposes port 2375, causing server attacks and solutions
>>: js implements shopping cart addition and subtraction and price calculation
01. Compile options and kernel compilation The Li...
This article shares the specific code of JavaScri...
Table of contents sequence 1. Centralized routing...
** Detailed graphic instructions for installing y...
1. Download mysql-5.7.21-windowx64.zip from the o...
Table of contents Main topic 1. Install Docker on...
On the server, in order to quickly log in to the ...
As shown in the figure: But when viewed under IE6...
We often need to control the hidden, transparent ...
This is a very important topic, not only for Linu...
Table of contents 1. Parent-child component commu...
Preface In the early stages of some projects, dev...
Table of contents Preface React Functional Compon...
Table of contents 01 Common controllers in k8s RC...
Apache Superset is a powerful BI tool that provid...