首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析SQLplus ORA错误

解析SQLplus ORA错误
EN

Stack Overflow用户
提问于 2013-01-29 15:50:26
回答 1查看 1K关注 0票数 0

我正在从bash运行几个sql脚本。我想在添加commit之前查看ORA错误的汇总。如下所示:

代码语言:javascript
复制
#!/bin/bash
S_DB2_CONNECTOR=""
echo "My statement"
SQL_STM=$( echo "UPDATE ..." | sqlplus login/pass@bd );
echo "Output:"
echo "$SQL_STM"
echo "searching for errors...."
echo $LOG_VAR | grep "ORA"
echo "before commit"
wait 1000
echo "COMMIT;" | sqlplus -s login/pass@bd;

但是这不起作用,因为sqlplus会话被中断了!sqlplus在SQL_STM执行后添加了自动提交。

如何在提交前解析sqlplus输出中的ORA-/ST-errors?最好在此终端屏幕中使用。

也许我不需要bash来解析,sqlplus可以为我做?(因此会话状态将被保留)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-29 15:57:00

如果您想在ORA代码的情况下回滚/执行一些额外的工作,那么在SQL*PLUS会话中完成所有这些工作。

例如,将其作为脚本运行。

代码语言:javascript
复制
set serverout on
begin
  update...;

exception
  when others -- others is a catch all, you can catch specific codes too
  then 
    rollback;
    dbms_output.put_line('Error!');
    dbms_output.put_line(sqlerrm); -- prints full error string
end;
/

如果您只想向bash发出sql语句失败的信号,那么您可以在sql*plus中将其设置为第一件事。取而代之的是whenever sqlerror exit sql.sqlcode (或whenever sqlerror (exit -1等)(参见here)。这将在出现第一个错误时停止,并返回到您的schell脚本,并返回适当的返回码。

你可以嵌套块,例如:

代码语言:javascript
复制
begin
  update ..;
  begin
    select id into v_id
      from tab
     where ...;
  exception
    when no_data_found
    then
      null;-- ignore that we didnt find a row
  end;
  -- if the select fails, we continue from here..
  delete...;
  begin
    savepoint mysave;
    your_proc(...);
  exception
   when others
   then
     rollback to mysave; -- of the call to your_proc fails, lets just toll that back alone
  end;

end;

等。

如果你需要它是交互式的,你可以做一些像(dbms_alert)http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_alert.htm#CHDCFHCI这样的事情

代码语言:javascript
复制
sqlplus /<<EOF | tee -a my.log
set feedback on verify on serverout on 
-- IE YOUR CODE HERE..
select * from dual;
begin
  null;
end;
/
-- END OF YOUR CODE..
-- now lets wait for an alert from another session:
declare
  v_message  varchar2(32767);
  v_status   number;
begin
  DBMS_ALERT.REGISTER('should_i_commit');
  DBMS_ALERT.WAITONE('should_i_commit', v_message, v_status); -- there is a timeout parameter you can set too
  if (v_message = 'Y')
  then
    dbms_output.put_line('I committed');
    commit;
  else
    dbms_output.put_line('I rolled back');
    rollback;
  end if;
end;
/
EOF

然后,在另一个会话中,您可以发出:

代码语言:javascript
复制
SQL> exec dbms_alert.signal('should_i_commit', 'N');

PL/SQL procedure successfully completed.

SQL> commit;

Commit complete.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14578034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档