PostgreSQL DROP PROCEDURE Statement
Summary: in this tutorial, you will learn how to use the PostgreSQL DROP PROCEDURE
statement to remove a procedure.
Introduction to PostgreSQL DROP PROCEDURE statement
The drop procedure
statement deletes one or more stored procedures from a database.
The following illustrates the syntax of the drop procedure
statement:
In this syntax:
- First, specify the name (
procedure_name
) of the stored procedure that you want to remove after thedrop procedure
keywords. - Second, use the
if exists
option if you want PostgreSQL to issue a notice instead of an error if you drop a stored procedure that does not exist. - Third, specify the argument list of the stored procedure if the stored procedure’s name is not unique in the database. Note that stored procedures that have different argument lists can share the same name. PostgreSQL needs the argument list to determine which stored procedure that you want to remove.
- Finally, use the
cascade
option to drop a stored procedure and its dependent objects, the objects that depend on those objects, and so on. The default option isrestrict
that will reject the removal of the stored procedure in case it has any dependent objects.
To drop multiple stored procedures, you specify a comma-separated list of stored procedure names after the drop procedure
keyword like this:
Creating sample stored procedures
Let’s create a couple of stored procedures that manage actors so that you can learn how to drop them:
The following insert_actor()
stored procedure inserts a new row into the actor
table. It accepts two arguments which are the first name and last name of the actor.
The following insert_actor
stored procedure also inserts a row into the actor
table. However, it accepts one argument which is the full name of the actor. The insert_actor()
uses the split_part()
function to split the full name into first name and last name before inserting them into the actor
table.
The following stored procedure deletes an actor by id:
The following stored procedure updates the first name and last name of an actor:
PostgreSQL Drop Procedure examples
First, attempt to drop the insert_actor
stored procedure:
PostgreSQL issued the following error:
Because there are two insert_actor
stored procedures, you need to specify the argument list so that PostgreSQL can select the right stored procedure to drop.
Second, drop the insert_actor(varchar)
stored procedure that accepts one argument:
Since the insert_actor
stored procedure is unique now, you can drop it without specifying the argument list:
It is the same as:
Third, remove two stored procedures using a single drop procedure
statement:
Summary
- Use the
drop procedure
statement to remove a stored procedure. - Specify a comma-separated list of stored procedure names after the
drop procedure
keywords to drop multiple stored procedures. - If the stored procedure name is not unique, use the argument list to specify which stored procedure you want to drop.