Home > Database > Mysql Tutorial > MySQL stored procedures

MySQL stored procedures

王林
Release: 2024-02-21 12:45:04
Original
1249 people have browsed it

MySQL stored procedures

MySQL stored procedures require specific code examples

In MySQL, a stored procedure is a precompiled procedure that is defined, saved and executed on the database server code. Stored procedures can accept parameters and return results, which makes them ideal for handling complex database operations and business logic.

Below, we will introduce the use of MySQL stored procedures through a specific code example.

Suppose we have an employee information table named "employees", which contains fields such as employee number, name, age, and department. We need to write a stored procedure to query employee information within a specified age range.

First, we need to create the "employees" table and insert some sample data:

CREATE TABLE employees (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  age INT NOT NULL,
  department VARCHAR(50) NOT NULL
);

INSERT INTO employees (name, age, department)
VALUES ('张三', 25, '销售部'),
       ('李四', 30, '人力资源部'),
       ('王五', 35, '技术部'),
       ('赵六', 40, '财务部');
Copy after login

Next, we can create a stored procedure to query employee information based on age range:

DELIMITER //

CREATE PROCEDURE GetEmployeesByAgeRange(IN minAge INT, IN maxAge INT)
BEGIN
  SELECT * FROM employees WHERE age >= minAge AND age <= maxAge;
END //

DELIMITER ;
Copy after login

In the above code, we use the DELIMITER keyword to specify the delimiter that ends the stored procedure definition. We then created a stored procedure named GetEmployeesByAgeRange using the CREATE PROCEDURE statement. This stored procedure accepts two input parameters, minAge and maxAge, which specify the age range. In the stored procedure body, we use the SELECT statement to query employee information that meets the conditions.

Finally, we use the DELIMITER keyword to restore the original statement delimiter.

Now, we can call the stored procedure to query employee information. Suppose we need to query employee information between the ages of 30 and 35, we can execute the following code:

CALL GetEmployeesByAgeRange(30, 35);
Copy after login

After running the above code, we will get the following results:

+----+------+-----+-----------------+
| id | name | age | department      |
+----+------+-----+-----------------+
|  2 | 李四 |  30 | 人力资源部      |
|  3 | 王五 |  35 | 技术部           |
+----+------+-----+-----------------+
Copy after login

Through the above example Code, we can see how to use MySQL stored procedures. Stored procedures can implement complex database operations by creating predefined blocks of code that can be called multiple times as needed.

To sum up, MySQL's stored procedure is a powerful database feature that can greatly improve developers' work efficiency and reduce duplicate code. Proficient in the use of stored procedures can enable us to handle various database operations and business logic more efficiently and flexibly.

The above is the detailed content of MySQL stored procedures. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template