MySQL CALL

The CALL statement in MySQL is used to execute a stored procedure. A stored procedure is a set of SQL statements that can be stored in the database and executed later. It allows you to encapsulate a series of SQL statements into a single named routine, providing modularity and reusability.

Syntax

Here’s the basic syntax for the CALL statement:

CALL procedure_name(argument1, argument2, ...);

procedure_name: The name of the stored procedure you want to execute.
argument1, argument2, …: The arguments or parameters required by the stored procedure, if any.

Example

Let’s consider an example where we have a simple stored procedure named calculate_sum that takes two parameters and returns their sum:

DELIMITER //
CREATE PROCEDURE calculate_sum(IN a INT, IN b INT)
BEGIN
    SELECT a + b AS sum_result;
END //
DELIMITER ;

Now, you can execute this stored procedure using the CALL statement:

CALL calculate_sum(5, 7);

In this example, the values 5 and 7 are passed as arguments to the calculate_sum stored procedure. The result will be a single-row result set with the sum, and you can handle or display the result as needed.

You can also use variables to capture the result of a stored procedure execution. For instance:

SET @result = 0;
CALL calculate_sum(5, 7) INTO @result;
SELECT @result AS result;

In this case, the result of the calculate_sum stored procedure is captured in the variable @result, and then it is selected for display.

Remember that the syntax and capabilities of stored procedures may vary based on the MySQL version and configuration. Always refer to the documentation corresponding to your MySQL version for accurate and up-to-date information.