简体   繁体   English

如何在oracle sql脚本中创建一个过程并在脚本中使用它?

[英]How to create a procedure in an oracle sql script and use it inside the script?

I want to create a script for my oracle DB , which drops tables . 我想为我的oracle DB创建一个脚本 ,它会丢弃表 If the table does not exist, the script won't exit as fail, just print a text: " does not exists ". 如果表不存在,脚本将不会退出失败,只需打印文本:“ 不存在 ”。

The script is the following: 该脚本如下:

BEGIN
    EXECUTE IMMEDIATE 'DROP TABLE mytable';
    DBMS_Output.Put_Line(' table dropped');
EXCEPTION WHEN OTHERS THEN
    IF SQLCODE = -942 THEN
        DBMS_Output.Put_Line(' table not exists');
    ELSE
        DBMS_Output.Put_Line(' Unknown exception while dropping table');
        RAISE;
    END IF;
END;

I want to drop a lot of table in one script, and I don't want to write these lines more than once. 我想在一个脚本中删除很多表,我不想多次写这些行。

Is there any way, to write it to a procedure or function which gets a parameter (the name of the table), and call this procedure in that script? 有没有办法,把它写入获取参数(表名)的过程或函数 ,并在该脚本中调用此过程?

Maybe something like this: 也许是这样的:

drop_table_procedure('mytableA');
drop_table_procedure('mytableB');

Or maybe a procedure, which gets an undefined size list (like in java: String ... table names ): 或者可能是一个过程,它获取一个未定义的大小列表(如在java: String ...表名中 ):

drop_tables_procedure('mytableA','mytableB');

Please give me some examples. 请举个例子。 Thanks! 谢谢!

Yes, you can declare a "temporary" procedure in an anonymous PL/SQL block: 是的,您可以在匿名PL / SQL块中声明“临时”过程:

DECLARE 

  PROCEDURE drop_if_exists(p_tablename VARCHAR)
  IS
  BEGIN
      EXECUTE IMMEDIATE 'DROP TABLE '||p_tablename;
      DBMS_Output.Put_Line(' table dropped');
  EXCEPTION WHEN OTHERS THEN
      IF SQLCODE = -942 THEN
          DBMS_Output.Put_Line(' table not exists');
      ELSE
          DBMS_Output.Put_Line(' Unknown exception while dropping table');
          RAISE;
      END IF;
  END;

BEGIN
  drop_if_exists('TABLE_1');
  drop_if_exists('TABLE_2');
END;
/

in execute immediate you need add name of database object. execute immediate您需要添加数据库对象的名称。 here's the script 这是脚本

create table t1 (col1 int);
create table t2 (col1 int);

create procedure drop_my_table(av_name varchar2)
as
begin
    EXECUTE IMMEDIATE 'DROP TABLE '||av_name;
    DBMS_Output.Put_Line(' table dropped');
EXCEPTION WHEN OTHERS THEN
    IF SQLCODE = -942 THEN
        DBMS_Output.Put_Line(' table not exists');
    ELSE
        DBMS_Output.Put_Line(' Unknown exception while dropping table');
        RAISE;
    END IF;

end drop_my_table;

declare
  type array_t is varray(2) of varchar2(30);
  atbls array_t := array_t('t1', 't2');
begin
  for i in 1..atbls.count loop
       drop_my_table(atbls(i));
   end loop; 
end;

You can use below one also 您也可以使用下面的一个

create or replace PROCEDURE drop_if_exists(p_tablename in VARCHAR)
      IS
    v_var1 number;
    begin 
    select 1 into v_var1 from user_tables where table_name=upper(p_tablename);
    if v_var1=1
    then 
    EXECUTE IMMEDIATE 'DROP TABLE '||p_tablename;
    DBMS_Output.Put_Line(' table dropped');
    else
    DBMS_Output.Put_Line(' table not exist');
    end if;
    exception 
    when others then
     DBMS_Output.Put_Line(' Unknown exception while dropping table');
            RAISE;
    end;

Call procedure 通话程序

    begin 
    drop_if_exists('emp');
    end;
    /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM