Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drop procedure if exists in DB Oracle

Can someone tell me how I can drop a PROCEDURE in Oracle, but just if it exists ?

DROP PROCEDURE IF EXISTS XYZ;

The above does not work.

like image 300
Fawi Avatar asked Feb 20 '14 14:02

Fawi


People also ask

How do you drop table only if exists Oracle?

IF EXISTS in Oracle Oracle does not provide IF EXISTS clause in the DROP TABLE statement, but you can use a PL/ SQL block to implement this functionality and prevent from errors then the table does not exist. This approach works faster and requires less resources from the database.

How do I drop a procedure in Oracle?

The syntax to a drop a procedure in Oracle is: DROP PROCEDURE procedure_name; procedure_name. The name of the procedure that you wish to drop.

Can we use DROP statement in Oracle procedure?

You cannot DROP tables in procedure using the DROP command. You need to use EXECUTE IMMEDIATE to run DDL commands in PL/SQL. Save this answer.

What is the statement to remove a procedure from a database?

The DROP PROCEDURE statement drops a standalone procedure from the database.


2 Answers

If your goal is to eliminate error messages in a script, then you can try

begin
   execute immediate 'drop procedure xyz';
exception when others then
   if sqlcode != -4043 then
      raise;
   end if;
end;
/
like image 77
René Nyffenegger Avatar answered Sep 23 '22 13:09

René Nyffenegger


You can also check dictionary view before:

SELECT * FROM USER_PROCEDURES WHERE PROCEDURE_NAME = 'XYZ'
like image 32
Wernfried Domscheit Avatar answered Sep 22 '22 13:09

Wernfried Domscheit