Home » SQL & PL/SQL » SQL & PL/SQL » Open Cursor for with an Exception Clause (11g Release 2 Windows)
Open Cursor for with an Exception Clause [message #608978] Thu, 27 February 2014 13:29 Go to next message
Duane
Messages: 557
Registered: December 2002
Senior Member
I might be missing something but can you have an exception with an OPEN (cursor name) FOR statement?

I'm trying to create a fail over so that if one database instance is down it fails over to the next database instance. It would appear this doesn't work. If an error occurs, such as, TNS error because they took the instance down to rebuild it, it doesn't fail over to the exception. Maybe you can't do this.


begin
  begin
    open Report for
      select emplid
        from database[b]1[/b]@somedblink
          where emplid = User;

  exception
    when others
      then
        begin
          open Report for
            select emplid
              from database[b]2[/b]@somedblink
                where emplid = User;
        exception
          when others
            then
              'Both Instances are down.  You are hosed'
        end;

exception
  when others
    then
      null;

end;

Re: Open Cursor for with an Exception Clause [message #608979 is a reply to message #608978] Thu, 27 February 2014 13:32 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

How can you know this does not work when you have:
exception
  when others
    then
      null;

This mean the block ALWAYS works whatever you do inside it.

Re: Open Cursor for with an Exception Clause [message #608980 is a reply to message #608979] Thu, 27 February 2014 13:36 Go to previous messageGo to next message
Duane
Messages: 557
Registered: December 2002
Senior Member
I just entered that in for my message. So if I took out the last exception then it would fail over (last exception with null)?
Re: Open Cursor for with an Exception Clause [message #608982 is a reply to message #608980] Thu, 27 February 2014 13:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Try it!

Re: Open Cursor for with an Exception Clause [message #608984 is a reply to message #608982] Thu, 27 February 2014 14:21 Go to previous messageGo to next message
Duane
Messages: 557
Registered: December 2002
Senior Member
Didn't work. Exception was "Invalid Number". This would be correct since I'm trying to make the first query fail so it rolls over to the Exception to try the next query in sequence. It would appear to roll all the way down to "Response := sqlerrm;" for the "Invalid Number".

Trying to help a co-worker out.

Actual code:


procedure getResult is

begin
    Response := GlobalVariables.Ok;
       
       begin   
         open var_Results for
           select 1 from dual@db1;
        
       exception
         when others
           then     
            open var_Results for
            select 1 from dual@db2;
        
             exception
               when others
                 then
                  /* this is from IRAPDW */      
                  begin
                    open var_Results for
                      select 1 from dual@db3;
        
                  exception
                    when others
                      then
                        Response := sqlerrm; 
                  end; 
             end; 
    end;
  end;



Mod Edit: Replaced actual Tables with example selects from dual.

[Updated on: Thu, 27 February 2014 19:49] by Moderator

Report message to a moderator

Re: Open Cursor for with an Exception Clause [message #608986 is a reply to message #608984] Thu, 27 February 2014 14:24 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
What datatype represents GlobalVariables.Ok?
Re: Open Cursor for with an Exception Clause [message #608987 is a reply to message #608986] Thu, 27 February 2014 14:28 Go to previous messageGo to next message
Duane
Messages: 557
Registered: December 2002
Senior Member
Sorry about that.

This should help.


CREATE OR REPLACE PACKAGE GlobalVariables is


  A               varchar2(1)   default 'A';
  Ok              varchar2(2)   default 'Ok';
  .
  .
  .
  .
  etc
  
end GlobalVariables;

Re: Open Cursor for with an Exception Clause [message #608988 is a reply to message #608987] Thu, 27 February 2014 14:32 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
See this example:
SQL> declare
  2    l_sum number(1);
  3  begin
  4    l_sum := 100 + 1000;
  5  exception
  6    when others then
  7      dbms_output.put_line(sqlerrm);
  8  end;
  9  /
ORA-06502: PL/SQL: numeric or value error: number precision too large

PL/SQL procedure successfully completed.

SQL>


The whole "ORA-06502 ..." line is result returned by the SQLERRM function. See how long it is? So, how do you plan to put all of that into a VARCHAR2(2) datatype variable?
Re: Open Cursor for with an Exception Clause [message #608989 is a reply to message #608988] Thu, 27 February 2014 14:38 Go to previous messageGo to next message
Duane
Messages: 557
Registered: December 2002
Senior Member
Response is an OUT parameter.


procedure getSSOName (var_emplid  in  varchar2, 
                      var_Results out sys_refcursor, 
                      Response    out varchar2)is


begin
    Response := GlobalVariables.Ok;
    .
    .
    .
    Response := sqlerrm; 

Re: Open Cursor for with an Exception Clause [message #608990 is a reply to message #608989] Thu, 27 February 2014 14:40 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
So what?

I mean, did you understand what I pointed to in my previous message?

[Updated on: Thu, 27 February 2014 14:41]

Report message to a moderator

Re: Open Cursor for with an Exception Clause [message #608991 is a reply to message #608990] Thu, 27 February 2014 14:47 Go to previous messageGo to next message
Duane
Messages: 557
Registered: December 2002
Senior Member
Sorry, I must be slow.

Are you saying that Response will default to 2 characters? Otherwise, I don't quite follow. Response first contains "Ok". If the last exception is executed then Response contains the error message.

Or, I'm totally missing what is written.
Re: Open Cursor for with an Exception Clause [message #608992 is a reply to message #608991] Thu, 27 February 2014 14:52 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Just for test, modify this line in your code:
Response := GlobalVariables.Ok;

to
Response := varchar2(200);

and run it again.

What happens?
Re: Open Cursor for with an Exception Clause [message #608993 is a reply to message #608992] Thu, 27 February 2014 14:55 Go to previous messageGo to next message
Duane
Messages: 557
Registered: December 2002
Senior Member
Doesn't compile.

--Response := GlobalVariables.Ok;

Response := varchar2(200);

[Error] PLS-00222 (2251: 17): PLS-00222: no function with name 'VARCHAR2' exists in this scope

Re: Open Cursor for with an Exception Clause [message #608994 is a reply to message #608993] Thu, 27 February 2014 15:03 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Ouch! You don't have to be sorry, but I must. Gee, what a blind fool I've been (still am). No idea how I managed to see what I saw (that RESPONSE is a variable that is being declared) - completely wrong. Sorry, I was talking nonsense ...

OK then, how do you call that procedure? RESPONSE, as its OUT parameter, has to be stored somewhere. What is that variable's datatype? Something like this:
declare
  l_response <what is here?>
begin
  getSSOName(some_emplid, blabla, L_RESPONSE);
Re: Open Cursor for with an Exception Clause [message #608995 is a reply to message #608994] Thu, 27 February 2014 15:19 Go to previous messageGo to next message
Duane
Messages: 557
Registered: December 2002
Senior Member
The procedure is part of a package and they have an ASP.net application that calls the package/procedure. I didn't include the package spec because I didn't think it was necessary.

Problem:
It's possible that a instance can be down. Rebuilding RPT from PROD. They want a way to roll over to another instance and grab the data they need without any interruption. From what they tell me, the begin/exception is not doing that and just returning the actual sqlerror. They want the CURSOR to always return data from some instance.

Test:
I'm breaking the first query to see if it will roll over to the next query. It would appear that is not happening.
Re: Open Cursor for with an Exception Clause [message #608996 is a reply to message #608995] Thu, 27 February 2014 15:45 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
I thought that code like this might have caused the problem:
SQL> create or replace procedure p_test (par_out out varchar2)
  2  is
  3  begin
  4    par_out := 'ORA-12345 some text here';
  5  end;
  6  /

Procedure created.

SQL> declare
  2    l_out number;      --> wrong! Should be VARCHAR2
  3  begin
  4    p_test (l_out);
  5    dbms_output.put_line(l_out);
  6  end;
  7  /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 4


SQL>

But, it doesn't return INVALID NUMBER but NUMERIC OR VALUE ERROR. It appears that INVALID NUMBER can be raised in SQL and not PL/SQL (am I wrong?). Unfortunately, I don't know ASP.net; if possible, see whether it, actually, returns RESPONSE out parameter into a variable whose datatype doesn't match VARCHAR2 (and can not implicitly be converted into it).
Re: Open Cursor for with an Exception Clause [message #608997 is a reply to message #608996] Thu, 27 February 2014 15:54 Go to previous messageGo to next message
Duane
Messages: 557
Registered: December 2002
Senior Member
Nothing to do with ASP.net. SQL error. From what I can tell, the query fails but it doesn't run the next exception but runs the bottom exception in the original query code.

           select some...columns....
             from some table 
               where empid  = 'my employee id'


ORA-01722: invalid number
ORA-02063: preceding line from db3



Mod Edit: removed column names

[Updated on: Thu, 27 February 2014 19:52] by Moderator

Report message to a moderator

Re: Open Cursor for with an Exception Clause [message #608998 is a reply to message #608997] Thu, 27 February 2014 15:57 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
what datatype is emplid?
Re: Open Cursor for with an Exception Clause [message #608999 is a reply to message #608998] Thu, 27 February 2014 15:58 Go to previous messageGo to next message
Duane
Messages: 557
Registered: December 2002
Senior Member
varchar2
Re: Open Cursor for with an Exception Clause [message #609000 is a reply to message #608999] Thu, 27 February 2014 16:42 Go to previous messageGo to next message
Duane
Messages: 557
Registered: December 2002
Senior Member
Ok, if anyone is still following, it would appear you CAN NOT use an exception with an OPEN FOR statement. If the query fails, it just returns the error. It will not execute the exception.

I'm hoping someone will prove me wrong and tell me to use THIS STATEMENT or THAT STATEMENT but for now, it won't work.
Re: Open Cursor for with an Exception Clause [message #609001 is a reply to message #608999] Thu, 27 February 2014 16:49 Go to previous messageGo to next message
ThomasG
Messages: 3211
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
The question might be *when* does it fail.

It could be that "opening and returning the cursor" works fine, only when you start to fetch rows the invalid number error happens, when the procedure itself is long finished.

The easiest way to debug that would be to try do build a simple test case that can be run directly from SQL*Plus. Have a look at the first comment "Just use SQL*Plus variable" in Kevins Blog here.

If you do it that way, you can figure out if the error happens when you *run* the procedure of when you *fetch* (print) the cursor data.



Re: Open Cursor for with an Exception Clause [message #609003 is a reply to message #609001] Thu, 27 February 2014 18:39 Go to previous message
Duane
Messages: 557
Registered: December 2002
Senior Member
Ah, that makes sense.

I'm also going to remove my queries for now. I'm not so sure our tables and database links should be posted in an open forum.

Darn, I can't edit those posts now. That's not good.

Who can I notify to remove/edit my queries?

[Updated on: Thu, 27 February 2014 18:40]

Report message to a moderator

Previous Topic: Get Min Value Where It Is Not Null
Next Topic: PIVOT - SQL Command not properly ended
Goto Forum:
  


Current Time: Thu Apr 25 08:14:32 CDT 2024