删除过程(如果DB Oracle中存在

bqujaahr  于 2023-01-20  发布在  Oracle
关注(0)|答案(4)|浏览(125)

有人能告诉我如何在Oracle中删除PROCEDURE,但前提是它存在?

DROP PROCEDURE IF EXISTS XYZ;

以上方法不起作用。

lztngnrs

lztngnrs1#

如果您的目标是消除脚本中的错误消息,则可以尝试

begin
   execute immediate 'drop procedure xyz';
exception when others then
   if sqlcode != -4043 then
      raise;
   end if;
end;
/
ars1skjm

ars1skjm2#

您还可以在以下之前检查字典视图:

SELECT * FROM USER_PROCEDURES WHERE PROCEDURE_NAME = 'XYZ'
hivapdat

hivapdat3#

我的解决方案:

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;
/
fcy6dtqo

fcy6dtqo4#

完整示例:

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;

相关问题