0
0
MysqlHow-ToBeginner · 3 min read

How to Call Stored Procedure in MySQL: Syntax and Example

To call a stored procedure in MySQL, use the CALL procedure_name(parameters); statement. This runs the stored procedure with any required input values and returns the results or performs actions defined inside it.
📐

Syntax

The basic syntax to call a stored procedure in MySQL is:

  • CALL: keyword to execute the procedure
  • procedure_name: the name of the stored procedure you want to run
  • (parameters): optional input values the procedure needs, separated by commas
  • ;: statement terminator
sql
CALL procedure_name(parameter1, parameter2, ...);
💻

Example

This example shows how to create a simple stored procedure that greets a user by name, then how to call it with a parameter.

sql
DELIMITER $$
CREATE PROCEDURE greetUser(IN userName VARCHAR(50))
BEGIN
  SELECT CONCAT('Hello, ', userName, '!') AS greeting;
END$$
DELIMITER ;

CALL greetUser('Alice');
Output
+------------+ | greeting | +------------+ | Hello, Alice! | +------------+
⚠️

Common Pitfalls

Common mistakes when calling stored procedures include:

  • Forgetting to use CALL keyword before the procedure name.
  • Not matching the number or type of parameters expected by the procedure.
  • Not setting the correct DELIMITER when creating procedures with multiple statements.
  • Trying to call procedures without proper permissions.
sql
/* Wrong: Missing CALL keyword */
greetUser('Alice');

/* Right: Use CALL keyword */
CALL greetUser('Alice');
📊

Quick Reference

CommandDescription
CALL procedure_name();Execute a stored procedure without parameters
CALL procedure_name(param1, param2);Execute a stored procedure with parameters
DELIMITER $$ ... DELIMITER ;Change delimiter to define multi-statement procedures
CREATE PROCEDURE ...Define a new stored procedure

Key Takeaways

Use the CALL keyword followed by the procedure name and parameters to run a stored procedure.
Match the number and type of parameters exactly as defined in the procedure.
Set DELIMITER properly when creating procedures with multiple statements.
Stored procedures can return results or perform actions inside the database.
Always check permissions to ensure you can execute the procedure.