【问题标题】:Convert PLSQL script in Unix?在 Unix 中转换 PLSQL 脚本?
【发布时间】:2013-07-22 16:28:49
【问题描述】:

是否可以将此 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;

有可能吗?我的意思是我有一个参数“测试”或“产品”。我必须进行插入,如果此插入有 DUPKEY,我必须将其写入日志。否则我会在日志中写“INSERT is ok”。上面的代码几乎是相同的概念,但在 oracle 中。我在 unix shell 中需要这个。谢谢。

【问题讨论】:

  • 'make an insert' where - 你的意思是调用 SQL*Plus 从 shell 脚本进行插入,并从那里将成功/失败消息记录到文件中? (为什么when others then raise?这似乎毫无意义)。
  • "do the insert from a shell script, and log the success/failure message to a file from there?" 是的!
  • 如果可能的话,从这段代码开始。这就是我想要的,但在 ksh 中
  • 哪个 Unix shell?伯恩?重击?正则表达式?科恩? C壳?灰?你需要更具体一点。
  • 是的,你是对的......但是 ksh

标签: oracle shell unix plsql ksh


【解决方案1】:

将您的程序创建为命名的 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 代码 sn-p 创建过程的示例: 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;

【讨论】:

  • 但我不明白如何通过 shell 脚本调用该过程。 (ksh Unix 外壳)。我必须写什么来调用它?因为我的区块是一个“匿名”区块,而且如您所知,它没有名字。抱歉,我从来没有使用过这种类型的积木。
  • 我已经添加了更多内容,应该可以更清楚地了解您需要做什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 2016-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
相关资源
最近更新 更多