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.
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;
/
You can also check dictionary view before:
SELECT * FROM USER_PROCEDURES WHERE PROCEDURE_NAME = 'XYZ'
My solution:
DECLARE
V_NUM NUMBER;
BEGIN
SELECT COUNT(*)
INTO V_NUM
FROM USER_OBJECTS
WHERE OBJECT_NAME = 'XYZ'
AND OBJECT_TYPE = 'PROCEDURE';
IF V_NUM > 0 THEN
EXECUTE IMMEDIATE 'DROP PROCEDURE XYZ';
DBMS_OUTPUT.PUT_LINE('Dropped');
END IF;
END;
/
A complete example:
declare
c int;
begin
select count(*) into c from user_procedures where object_type = 'FUNCTION' and object_name = 'ABC';
if c = 1 then
execute immediate 'DROP FUNCTION ABC';
end if;
end;