oracle中的No_Data_found异常

时间:2019-10-13 19:56:19

标签: oracle exception plsql

我有两个数据库表(account和account1)。我想输入一个帐号,首先检查是否 帐号是否在帐号表中。如果不在帐号表中,则引发异常并检查account1表。代码适用于此。但是,如果我想显示错误消息,如果在account1表中也找不到该帐号,那我该怎么办。 这是我的部分代码-

BEGIN
begin
select Balance,Cid
    into cur_balance,cl_id
    from Account 
    where Accno = x;
    select max(Tid) into id from(select Tid from trnsaction union select Tid from trnsaction1);
    id:=id+1;
    ac_branch := 'mirpur';
exception
    when no_data_found then
    select Balance,Cid
    into cur_balance,cl_id
    from account1
    where accno = x;

    select max(Tid) into id from(select Tid from trnsaction union select Tid from trnsaction1);
    id:=id+1;

    ac_branch := 'gulshan';         

end;

2 个答案:

答案 0 :(得分:1)

您可以使用

  

raise_application_error(-20001,'None of the tables contain this account !');   

     

dbms_output.put_line('None of the tables contain this account !');

Account表的最后一个引发的异常语句中:

DECLARE
  cur_balance  Account.Balance%type;
  cl_id        Account.Cid%type;
  id           trnsaction.Tid%type;
BEGIN
  begin
        select Balance, Cid
          into cur_balance, cl_id
          from Account
         where Accno = x;

        ac_branch := 'mirpur';
    exception
      when no_data_found then
        select Balance, Cid
          into cur_balance, cl_id
          from Account1
         where accno = x;

      ac_branch := 'gulshan'; 

        exception
          when no_data_found then 
            raise_application_error(-20001,'None of the tables contain this account !');           
  end;     

  begin
      select max(Tid)
        into id
        from (select Tid
                from trnsaction
              union
              select Tid from trnsaction1);
      id := id + 1;

     exception
       when no_data_found then null;           
  end;    
END;

AccountAccount1的列类型以及trnsaction1trnsaction的表的列类型被认为相对相同。

如果您更喜欢使用dbms_output.put_line,请在其前发出命令set serveroutput on

顺便说一句,无需重复其他与我们感兴趣的查询无关的查询。

答案 1 :(得分:0)

union allrow_number为什么不显示?

begin
Select Balance,Cid, branch
into cur_balance,cl_id, ac_branch
from
(Select Balance,Cid, branch
       row_number() 
       Over (order by seq) as rn
From   
(select Balance,Cid, 1 as seq, 'mirpur' as branch
    from Account 
    where Accno = x
Union all
select Balance,Cid, 2 as seq, 'gulshan' as branch
    from account1
    where accno = x))
Where rn = 1;
    select max(Tid) into id from(select Tid from trnsaction union select Tid from trnsaction1);
    id:=id+1;
exception
    when no_data_found then
    Null; -- or dbms_output.put_line('acct not found');
end;
/

干杯!

相关问题