CALL

CALL in SQL is a command used to execute a stored procedure within a database. It runs the stored procedure exactly as it is designed without requiring the user to type the whole procedure each time it is needed. It helps to simplify and optimize database operations. The procedures to be called should already exist within the database structure.

Example

CALL procedure_name();

Output

Query OK, 0 rows affected (0.00 sec)

Explanation

The CALL command is used to execute a stored procedure in MySQL that has been defined previously. In this syntax, procedure_name() is the name of the stored procedure to be executed. The result of the CALL command indicates whether the procedure was executed successfully without any error. The message Query OK, 0 rows affected denotes that the stored procedure was executed successfully.

Example

CREATE PROCEDURE GetEmployee @ID INT AS
BEGIN
SELECT * FROM Employees WHERE EmployeeId = @ID
END;
GO
CALL GetEmployee 1;

Output

| EmployeeId | Name | JobTitle |
|------------|--------|----------|
| 1 | John | Manager |

Explanation

In the above code, a stored procedure named “GetEmployee” is created that takes one parameter and queries the “Employees” table for the row where the “EmployeeId” matches the input parameter. The “CALL” statement is then used to execute this stored procedure, providing “1” as the parameter. The output is as shown in the ‘Output’ section.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.