MySQL Stored Procedure Creation: Using DELIMITER and DROP PROCEDURE IF EXISTS

Dec 11, 2025 · Programming · 10 views · 7.8

Keywords: MySQL | Stored Procedure | DELIMITER | DROP PROCEDURE | Parameters

Abstract: This article details the method of using DELIMITER to change statement delimiters when creating stored procedures in MySQL, and how to use DROP PROCEDURE IF EXISTS to avoid errors when the procedure already exists. Through example code, it demonstrates the complete creation and calling process, suitable for database developers and technology enthusiasts.

Introduction

Stored procedures are important database objects in MySQL that allow encapsulating a series of SQL statements into a single unit to improve code reusability and performance. When creating stored procedures, correctly using DELIMITER and DROP PROCEDURE IF EXISTS is key to avoiding common errors.

Necessity of DELIMITER

The default statement delimiter in MySQL is the semicolon (;). When defining a stored procedure, the procedure body may contain multiple SQL statements ending with semicolons. To distinguish the end of the entire stored procedure definition from the end of internal statements, it is necessary to temporarily change the delimiter. Using the DELIMITER command achieves this, for example:

DELIMITER $$

This changes the delimiter to $$, allowing subsequent CREATE PROCEDURE statements to use semicolons without immediate execution.

Complete Syntax for Creating Stored Procedures

To safely create stored procedures, it is recommended to first use DROP PROCEDURE IF EXISTS to delete any existing procedure with the same name, then create it. A typical example is as follows:

DELIMITER $$

DROP PROCEDURE IF EXISTS `sp_user_login` $$
CREATE DEFINER=`root`@`%` PROCEDURE `sp_user_login`(
  IN loc_username VARCHAR(255),
  IN loc_password VARCHAR(255)
)
BEGIN
  SELECT user_id, user_name, user_emailid, user_profileimage, last_update
  FROM tbl_user
  WHERE user_name = loc_username
    AND password = loc_password
    AND status = 1;
END $$

DELIMITER ;

In this example, the stored procedure sp_user_login accepts two IN parameters and returns matching user information.

Calling Stored Procedures

After creation, stored procedures can be called using the CALL statement:

CALL sp_user_login('testuser', 'password123');

Parameter Type Extensions

Besides IN parameters, MySQL stored procedures also support OUT and INOUT parameters. For instance, OUT parameters are used to return results, while INOUT parameters can serve as both input and output. From supplementary references, it can be defined as follows:

DELIMITER //

CREATE PROCEDURE foobar(OUT var1 VARCHAR(100))
BEGIN
  SET var1 = "example output";
END //

DELIMITER ;

Best Practices

In development, always use DROP PROCEDURE IF EXISTS to avoid errors, and restore the default delimiter promptly after using DELIMITER. Additionally, designing parameter types appropriately can enhance the flexibility of stored procedures.

Conclusion

This article has detailed the method of using DELIMITER and DROP PROCEDURE IF EXISTS when creating stored procedures in MySQL. By correctly applying these techniques, developers can efficiently manage database logic and improve application performance.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.