Creating, Altering, and Dropping Stored Procedures
Stored procedures are an essential built-in function or feature of many relational database management system (RDBMS) software, such as MySQL, PostgreSQL, and SQL Server. Stored procedures allow database users to store a series of SQL query command statements that can be executed as a single unit within the database system, simplifying custom user-defined database task operations, reusing database query source code, and improving performance by reducing the need to parse database queries.

Creating a Stored Procedure in a Database Management System.
Creating a Stored Procedure in MySQL.
To create a stored procedure in the MySQL database management system, the CREATE PROCEDURE query command statement is used. The database user can specify input (IN), output (OUT), or input/output (INOUT) parameters.
Syntax of a MySQL Stored Procedure.
CREATE PROCEDURE procedure_name([parameter1 datatype, parameter2 datatype, …])
BEGIN
— Your desired SQL query command statement
END;
Example of a MySQL Stored Procedure.
CREATE PROCEDURE GetEmployeeData(IN emp_id INT)
BEGIN
SELECT emp_name, designation, pay
FROM employee
WHERE employee_id = emp_id;
END;
In this example, we create a MySQL Stored Procedure named GetEmployeeData, which takes the employee ID (emp_id) value in the existing Employee table as input, and returns detailed information about the employee’s name, designation, and pay from the Employee table.
Creating a Stored Procedure in PostgreSQL.
In the PostgreSQL database management system, database users use the CREATE FUNCTION query command statement to create a stored procedure (known as functions in PostgreSQL). PostgreSQL also uses a different syntax to declare and define these functions.
Syntax of a PostgreSQL Stored Procedure.
CREATE OR REPLACE FUNCTION function_name(parameter1 datatype, parameter2 datatype)
RETURNS return_type AS $$
BEGIN
— Your desired SQL query command statement
RETURN value;
END;
$$ LANGUAGE plpgsql;
Example of a PostgreSQL Stored Procedure.
CREATE OR REPLACE FUNCTION GetEmployeeData(emp_id INT)
RETURNS TABLE(emp_name TEXT, designation TEXT, pay NUMERIC) AS $$
BEGIN
RETURN QUERY
SELECT emp_name, designation, pay
FROM employee
WHERE employee_id = emp_id;
END;
$$ LANGUAGE plpgsql;
In this example, it creates a function GetEmployeeData that takes an employee ID value as input and returns a result set containing employee table data information (emp_name, designation, pay).
Creating a Stored Procedure in SQL Server.
In the SQL Server database management system, database users use the CREATE PROCEDURE query command statement to create a stored procedure, similar to the MySQL database software.
Syntax of a SQL Server Stored Procedure.
CREATE PROCEDURE procedure_name (@parameter1 datatype, @parameter2 datatype, …)
AS
BEGIN
— Your desired SQL query command statement
END;
Example of a SQL Server Stored Procedure.
CREATE PROCEDURE GetEmployeeData
@emp_id INT
AS
BEGIN
SELECT emp_name, designation, pay
FROM employee
WHERE employee_id = @emp_id;
END;
In this example, SQL Server creates a stored procedure named GetEmployeeData, which takes emp_id as an input parameter and provides the employee’s name, designation, and pay details information from the Employee table.
Modifying a Stored Procedure Created in Your Existing Database Software.
In some database systems, a stored procedure can be updated or modified after it has been created using the ALTER PROCEDURE query command statement. However, this command is not fully supported in most cases. Instead, most RDBMSs require the database user to delete the existing procedure and create a new database table procedure.
MySQL Modifying a Stored Procedure Concept.
In MySQL database software, database users cannot directly update or modify a stored procedure. However, to perform an update, the database user must delete the existing MySQL-created procedure and create a new procedure with the updated definition.
Steps for modifying in MySQL database software.
Delete your existing database stored procedure data.
DROP PROCEDURE IF EXISTS procedure_name;
With this, create a new procedure with your new custom updated logic expression.
PostgreSQL Modifying a Stored Procedure Concept.
In PostgreSQL database software, database users can modify a function using the CREATE OR REPLACE FUNCTION syntax. This function essentially deletes your existing function and creates a new one.
Example of steps for modifying in PostgreSQL database software.
CREATE OR REPLACE FUNCTION GetEmployeeData(emp_id INT)
RETURNS TABLE(emp_name TEXT, designation TEXT, pay NUMERIC) AS $$
BEGIN
RETURN QUERY
SELECT emp_name, designation, pay
FROM employee
WHERE employee_id = emp_id;
END;
$$ LANGUAGE plpgsql;
This command query statement replaces your existing GetEmployeeData function with the new definition.
SQL Server Modifying a Stored Procedure Concept.
In SQL Server database software, database users cannot use the ALTER PROCEDURE command to replace their existing stored procedure. Instead, the database user must delete the previously created stored procedure and recreate it with the new definition logic expression.
Example of steps for modifying in SQL Server database software.
Delete your existing stored procedure data information.
DROP PROCEDURE IF EXISTS GetEmployeeData;
Then, the database user creates a new procedure information with an updated logic expression.
Deleting a previously created stored procedure in database software.
To delete a stored procedure created by a database user in different database software, you apply the DROP PROCEDURE (or DROP FUNCTION in PostgreSQL) command statement.
Deleting a previously created stored procedure MySQL.
DROP PROCEDURE IF EXISTS procedure_name;
If it contains the name of a previously created stored procedure procedure_name, it will delete it immediately.
Deleting a previously created stored procedure PostgreSQL.
DROP FUNCTION IF EXISTS function_name;
If it contains the name of a previously created function (stored procedure) function_name, it will delete it immediately.
SQL Server
DROP PROCEDURE IF EXISTS procedure_name;
If it contains the name of a previously created stored procedure procedure_name, it will delete it immediately.
Here’s a step-by-step example of creating, changing, and deleting a stored procedure.
Here’s a step-by-step example of creating, modifying, and deleting a new stored procedure in database software.
Step 1: Creating a New Stored Procedure.
– Create a procedure in MySQL database software.
CREATE PROCEDURE GetEmployeeData(IN emp_id INT)
BEGIN
SELECT emp_name, designation, pay
FROM employee
WHERE employee_id = emp_id;
END;
Step 2: Modify an already created stored procedure as needed.
Delete your existing procedure.
DROP PROCEDURE IF EXISTS GetEmployeeData;
Now create an updated procedure with new SQL logic or an additional parameter.
CREATE PROCEDURE GetEmployeeData(IN emp_id INT, IN organization VARCHAR(130))
BEGIN
SELECT emp_name, designation, pay
FROM employee
WHERE employee_id = emp_id AND organization = organization;
END;
Step 3: Dropping your existing stored procedure.
DROP PROCEDURE IF EXISTS GetEmployeeData;
Conclusion of Creating, Altering, and Dropping Stored Procedures.
- MySQL – Use the CREATE PROCEDURE query command to create a new stored procedure in database software. There is no direct ALTER PROCEDURE support. Instead, drop your existing procedure and create a new stored procedure.
- PostgreSQL – Use the CREATE OR REPLACE FUNCTION query command to create and modify a new stored procedure in database software.
- SQL Server – Use the CREATE PROCEDURE query command to create a new stored procedure in database software. To modify an existing stored procedure, first drop the stored procedure and then recreate it.
