Oracle SQLplus ORA错误解析

Oracle SQLplus ORA错误解析,oracle,bash,sqlplus,Oracle,Bash,Sqlplus,我正在运行来自bash的几个sql脚本。在添加commit之前,我想看看ORA错误的总结。大概是这样的: #!/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"

我正在运行来自bash的几个sql脚本。在添加commit之前,我想看看ORA错误的总结。大概是这样的:

#!/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错误?最好在这个终端屏幕上


也许我不需要bash进行解析,sqlplus可以帮我做吗?因此,会话状态将被保留。

如果您想在ORA代码中回滚/执行一些额外的操作,请在SQL*PLUS会话中执行所有操作

i、 e.将其作为脚本运行

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中的第一件事。每当sqlerror退出sql.sqlcode或sqlerror退出-1等时,请参见。这将在第一个错误时停止,并使用适当的返回代码返回到schell脚本

您可以嵌套块,例如:

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.htmCHDCFHCI]:

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

我想自己做决定。错误不会自动发生。有些ORA错误是可以的,有些不是。谢谢你的建议。不幸的是,我发现我的sql知识比bash差。请你解释一下,这个代码到底是做什么的?dbms_output.put_linesqlerrm;它会在sql提交请求之前正常执行sql代码并输出所有ORA错误吗?@idobr如果一条语句失败,它将直接进入when others块,并运行其中的任何内容,即它不会再处理begin部分的任何代码。感谢您的关注。所以即使是因为微不足道的错误,它也会破坏脚本的执行,不是吗?我明白了我可以说明的一点,哪些错误代码是重要的。我可以不中断地将所有错误写入某个位置,然后在提交决策之前输出吗?@idobr是的,通过嵌套捕获该错误的开始/结束块,您可以轻松忽略一些小错误。例如开始更新;从选项卡开始将id选择为空;未找到数据时出现异常,然后为空;终止删去终止如果select失败且未找到行,则执行将继续到delete,因为只有内部begin end块失败。我正在旅行,所以目前无法正确更新我的答案,但如果你愿意,我可以很快添加一个更复杂的例子。这篇文章可能对某人有用。
SQL> exec dbms_alert.signal('should_i_commit', 'N');

PL/SQL procedure successfully completed.

SQL> commit;

Commit complete.