mysql
  1. mysql-procedure

Procedures in MySQL

Procedures in MySQL are a way to encapsulate a set of SQL statements into a reusable stored procedure that can be called from various parts of your application. In this tutorial, we'll explore the syntax and usage of procedures in MySQL.

Syntax

To define a procedure in MySQL, you use the CREATE PROCEDURE statement, followed by the procedure name and its parameters (if any). Here's the basic syntax:

CREATE PROCEDURE procedure_name (IN param1_type param1_name, IN param2_type param2_name, ...)
BEGIN
    -- SQL statements
END;

The IN keyword before the parameter name specifies that the parameter is an input parameter. You can also use OUT and INOUT to specify output and input/output parameters.

Example

Let's say we want to create a procedure in MySQL to get the total number of customers in a given city. Here's how we can implement it:

CREATE PROCEDURE `get_customer_count`(IN p_city varchar(255), OUT p_count int)
BEGIN
    SELECT COUNT(*) INTO p_count FROM customers WHERE city = p_city;
END;

In this example, the procedure takes an input parameter p_city of type varchar(255) and an output parameter p_count of type int. The procedure uses a SELECT statement to get the total number of customers in the specified city and stores the result in the output parameter p_count.

Output

To execute the procedure and get the output, we can use the following MySQL statement:

CALL get_customer_count('New York', @count);
SELECT @count;

This statement calls the get_customer_count procedure with the input parameter New York and stores the output in a temporary variable @count. The second statement then selects the value of @count, which should contain the total number of customers in New York.

Explanation

In the above example, we defined a procedure called get_customer_count that takes an input parameter p_city of type varchar(255) and an output parameter p_count of type int. The procedure then selects the total number of rows from the customers table where the city column matches the input parameter p_city, and stores the result in the output parameter p_count. We then used the CALL statement to execute the procedure and store the output in a temporary variable @count, which was then selected to display the result.

Use

Procedures in MySQL are useful for encapsulating a set of SQL statements into a reusable block of code that can be called from anywhere in your application. This can help simplify your code and improve maintainability. Procedures can also help improve performance by reducing the number of network round-trips between your application and the database.

Important Points

  • Procedures in MySQL are defined using the CREATE PROCEDURE statement.
  • Parameters can be specified as IN, OUT, or INOUT.
  • Procedures can contain any valid SQL statements, including selection, insertion, deletion, and updates.
  • Procedures can be called using the CALL statement.

Summary

In this tutorial, we explored the syntax and usage of procedures in MySQL. We discussed how to define a procedure, how to call a procedure, and some of the benefits of using procedures in your application. With this knowledge, you can now use procedures to simplify your MySQL code and improve its maintainability and performance.

Published on: