How to Drop Procedure in SQL
Last Updated :
13 Jan, 2025
In SQL, stored procedures are used to encapsulate complex logic and queries. However, there may come a time when we need to remove or delete a stored procedure from a database.
In this article, we will cover the various methods for dropping a stored procedure in SQL along with examples and explanations.
Drop a Stored Procedure
A stored procedure is a set of SQL statements that are saved and can be run together as a single task.
Sometimes, we may need to drop a stored procedure either because it is no longer needed or because we need to modify it. Dropping a stored procedure means permanently removing it from the database.
In SQL Server and most relational database management systems (RDBMS), we use the DROP PROCEDURE command to delete a stored procedure.
Basic Syntax for Dropping a Procedure
The syntax to drop a stored procedure is quite simple:
DROP PROCEDURE procedure_name;
Where:
procedure_name is the name of the stored procedure that we want to remove.
For example, if we have a procedure called GetEmployeeDetails, we can drop it using the following command:
Query:
DROP PROCEDURE GetEmployeeDetails;
How to drop procedure in SQL?
Lets discuss some approach through which we can drop procedure in SQL are defined below:
1. Dropping a Procedure If It Exists
In some cases, we might want to ensure that the stored procedure exists before attempting to drop it. This prevents errors in case the procedure doesn't exist. To handle this, we can use the IF EXISTS clause (supported in SQL Server, MySQL, and other RDBMS).
Query:
IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'GetEmployeeDetails')
BEGIN
DROP PROCEDURE GetEmployeeDetails;
END;
2. Dropping Multiple Procedures
We can drop multiple procedures in a single statement by separating their names with commas. This method is useful if we need to clean up several stored procedures at once.
DROP PROCEDURE IF EXISTS Procedure1, Procedure2, Procedure3;
This allows us to efficiently manage multiple procedures at once without writing separate DROP PROCEDURE commands for each.
3. Dropping a Procedure in a Specific Schema
In databases that use schemas (like SQL Server or PostgreSQL), we may need to specify the schema along with the procedure name.
This ensures that the correct procedure is dropped if there are multiple procedures with the same name in different schemas.
Conclusion
Dropping a stored procedure in SQL is straightforward, but it's important to take precautions to avoid accidental deletion of important procedures. Always use the IF EXISTS check to ensure the procedure exists before trying to drop it, and be aware of any dependencies that might be impacted. By following best practices and using the appropriate commands for your SQL Server or MySQL environment, you can manage stored procedures effectively.
Similar Reads
How to SELECT FROM Stored Procedure in SQL
Stored procedures are precompiled SQL queries stored in the database that encapsulate logic and can accept parameters, perform operations and return results. They are widely used in SQL for encapsulating reusable logic, improving performance and enhancing security. In this article, weâll explore how
4 min read
How to Use Stored Procedure in SQLite?
SQLite is a popular, lightweight, self-contained, serverless, and open-source relational database management system (RDBMS) that is widely used in various applications. SQLite does not directly support stored procedures like other database management systems (DBMS) such as MySQL or PostgreSQL. To ac
4 min read
How to Create and Call a Stored Procedure in SQL?
With this article, we will learn how to Create and Call a Stored Procedure in SQL. For this article, we are going to use MSSQL as our database server. What is a Stored Procedure?A stored procedure is a pre-written SQL query that can be called multiple times and will run as the same. Like we can crea
2 min read
Procedures in PL/SQL
PL/SQL procedures are reusable code blocks that perform specific actions or logic within a database environment. They consist of two main components such as the procedure header which defines the procedure name and optional parameters and the procedure body which contains the executable statements i
5 min read
How to Limit Rows in a SQL Server?
To limit rows in SQL Server, use the TOP clause in the SELECT statement. Using the TOP clause in SQL Server, users can limit the number of rows in the results set. Here, we will understand how to limit rows in SQL Server with the help of different examples. Steps to Limit Rows in SQL ServerLet's che
3 min read
SQL Stored Procedures
Stored procedures are precompiled SQL statements that are stored in the database and can be executed as a single unit. SQL Stored Procedures are a powerful feature in database management systems (DBMS) that allow developers to encapsulate SQL code and business logic. When executed, they can accept i
7 min read
How to Modify a Stored Procedure in SQL Server?
In this article, we will learn to modify the created stored procedure in MS SQL.You can modify the Stored Procedure in two ways. one is by using a client called SSMS and other way is by using T-SQL statements + SSMS in MS SQL Server. Method 1: Using SQL Server Management Studio (SSMS) to Modify the
3 min read
How to Rename a Table in Oracle SQL?
Renaming a table in Oracle SQL can be done in different ways depending on the context and the tools we are using. In this article, we will explore the main methods for renaming a table by including the basic RENAME command and alternative approaches like using SQL Developer or renaming a table with
3 min read
How to Drop Row in Polars - Python
Polars is a fast and efficient DataFrame library designed for handling large datasets in Python. While Pandas is the go-to for many, Polars is gaining traction due to its performance advantages, especially with larger datasets. If we're transitioning from Pandas or exploring Polars for our data mani
4 min read
Drop login in SQL Server
The DROP LOGIN statement could be used to delete an user or login that used to connect to a SQL Server instance. Syntax - DROP LOGIN loginname; GO Permissions : The user which is deleting the login must have ALTER ANY LOGIN permission on the server. Note - A login which is currently logged into SQL
1 min read