Home >Database >Mysql Tutorial >How do you create a stored procedure in MySQL using the CREATE PROCEDURE statement?
To create a stored procedure in MySQL, you use the CREATE PROCEDURE
statement. Here's a step-by-step guide on how to do this:
CREATE PROCEDURE
followed by the name of your procedure.IN
, OUT
, or INOUT
, and each parameter needs a data type.BEGIN
and END
block. Within this block, you write the SQL statements that the procedure will execute.;
) as a statement terminator. However, when creating stored procedures, you need to temporarily change this delimiter to avoid conflicts with the semicolons used in the procedure body. Typically, //
or $$
are used as alternative delimiters.Here's an example of how to create a simple stored procedure:
<code class="sql">DELIMITER // CREATE PROCEDURE GetEmployeeData( IN employeeID INT ) BEGIN SELECT * FROM employees WHERE id = employeeID; END // DELIMITER ;</code>
In this example, GetEmployeeData
is a procedure that takes an employeeID
as an input parameter and returns all the data for that employee from the employees
table.
MySQL stored procedures can include parameters of various types and modes. The parameters can be categorized into three modes:
IN
parameter allows you to pass a value into the procedure. The procedure can read this value but cannot modify it.OUT
parameter allows the procedure to return a value to the calling environment. The procedure can modify the value of an OUT
parameter, and the modified value is accessible after the procedure execution.INOUT
parameter combines the properties of IN
and OUT
. It allows you to pass a value into the procedure and also allows the procedure to modify the value and return it to the calling environment.Parameters can also have data types such as INT
, VARCHAR
, DATE
, etc., which should be specified when declaring them. Here's an example of a stored procedure with all three types of parameters:
<code class="sql">DELIMITER // CREATE PROCEDURE ProcessEmployeeData( IN empID INT, OUT empName VARCHAR(100), INOUT empSalary DECIMAL(10, 2) ) BEGIN SELECT first_name, last_name INTO empName FROM employees WHERE id = empID; UPDATE employees SET salary = empSalary 1000 WHERE id = empID; SELECT salary INTO empSalary FROM employees WHERE id = empID; END // DELIMITER ;</code>
To modify an existing stored procedure in MySQL, you use the ALTER PROCEDURE
statement. However, ALTER PROCEDURE
is limited to changing certain attributes of the procedure, such as its characteristics (like security context or SQL security). To modify the actual code or logic within the procedure, you need to use the DROP PROCEDURE
and CREATE PROCEDURE
statements.
Here’s how to do it:
<code class="sql">DROP PROCEDURE IF EXISTS ProcedureName;</code>
<code class="sql">DELIMITER // CREATE PROCEDURE ProcedureName( -- Parameters ) BEGIN -- New procedure body END // DELIMITER ;</code>
For example, if you want to update the GetEmployeeData
procedure to return only the first and last names of an employee, you would do the following:
<code class="sql">DROP PROCEDURE IF EXISTS GetEmployeeData; DELIMITER // CREATE PROCEDURE GetEmployeeData( IN employeeID INT ) BEGIN SELECT first_name, last_name FROM employees WHERE id = employeeID; END // DELIMITER ;</code>
Stored procedures offer several benefits for database management in MySQL:
By leveraging these benefits, stored procedures can significantly enhance the efficiency and security of database management in MySQL.
The above is the detailed content of How do you create a stored procedure in MySQL using the CREATE PROCEDURE statement?. For more information, please follow other related articles on the PHP Chinese website!