在Unix中转换PLSQL脚本?

时间:2013-07-23 14:23:14

标签: oracle shell unix plsql ksh

是否可以将此PL-SQL代码转换为unix代码?

declare
   tipE varchar(8) := 'TEST';
begin
insert into TABLENAME VALUES (values);
   if tipExec = 'TEST' then
      dbms_output.put_line('INSERT is ok; called ROLLBACK in '' TEST');
      ROLLBACK;
   end if;

  exception
    when DUP_VAL_ON_INDEX then
      if tipE = 'TEST' then
         raise_application_error(-9999, 'DUPKEY in'' TEST');
      else
         raise;
      end if;
    when others then
      raise;
end;

有可能吗?我的意思是我有一个参数“test”或“prod”。我必须插入一个插件,如果这个插件有DUPKEY,我必须在日志中写入它。否则我会在日志中写入“INSERT is ok”。而上面的代码几乎是相同的概念,但在oracle中。我需要在unix shell中使用它。感谢。

1 个答案:

答案 0 :(得分:0)

将上面的程序创建为一个命名的PL / SQL过程,然后您可以通过Unix shell脚本轻松地从sqlplus调用它,如下所示:

sqlplus -s $USERPASS  << sqlend
    whenever sqlerror exit 1
    set heading off
    exec your_function('$example_variable');
sqlend

...其中$ USERPASS是您的登录机制/用户和密码。如果你不使用任何函数参数,只需删除括号中的那一位。

请查看此Oracle教程,了解如何从PLSQL代码段创建过程的示例: http://www.oracle.com/technetwork/issue-archive/2011/11-mar/o21plsql-242570.html

作为初学者,你的程序看起来像这样(未经测试):

CREATE OR REPLACE PROCEDURE your_function
IS
   tipE varchar(8) := 'TEST';
begin
insert into TABLENAME VALUES (values);
   if tipExec = 'TEST' then
      dbms_output.put_line('INSERT is ok; called ROLLBACK in '' TEST');
      ROLLBACK;
   end if;

  exception
    when DUP_VAL_ON_INDEX then
      if tipE = 'TEST' then
         raise_application_error(-9999, 'DUPKEY in'' TEST');
      else
         raise;
      end if;
    when others then
      raise;
end your_function;