Home » SQL & PL/SQL » SQL & PL/SQL » Exception Handling For %type , %Rowtype variables (Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit ProductionE)
Exception Handling For %type , %Rowtype variables [message #620429] Fri, 01 August 2014 04:33 Go to next message
saipradyumn
Messages: 419
Registered: October 2011
Location: Hyderabad
Senior Member
Hi All,

Oracle has support the unique exceptional behavior all kinds type variables.
But after raising the exception also one of the attribute of the ROWTYPE variable holds valid value,
where as TYPE variable doesn't hold any value

Please find the below example :


SQL> declare
  2    emp_rowtype scott.emp%rowtype;
  3    empno_type  scott.emp.empno%type;
  4  begin
  5    begin
  6  
  7        dbms_output.put_line(' Initial value ' || empno_type);
  8      select e.empno into empno_type from scott.emp e where e.deptno = 10;
  9    exception
 10      when others then
 11        dbms_output.put_line('Error message with  %type    :' || sqlerrm);
 12        dbms_output.put_line(' Exceptional block ' || empno_type);
 13    end;
 14  
 15    begin
 16      dbms_output.put_line(' Before assigning ename  ' || emp_rowtype.ename);
 17      select e.* into emp_rowtype from scott.emp e where e.deptno = 20;
 18  
 19    exception
 20      when others then
 21        dbms_output.put_line('Error message with %rowtype  :' || sqlerrm);
 22  
 23        dbms_output.put_line(' After Raising  Exception  Ename ' ||
 24                             emp_rowtype.ename);
 25        dbms_output.put_line(' After Raising  Exception  empno ' ||
 26                             emp_rowtype.empno);
 27        dbms_output.put_line(' After Raising  Exception  sal ' ||
 28                             emp_rowtype.sal);
 29    end;
 30  end;
 31  /
 
 Initial value 
Error message with  %type    :ORA-01422: exact fetch returns more than requested number of rows
 Exceptional block 
 Before assigning ename  
Error message with %rowtype  :ORA-01422: exact fetch returns more than requested number of rows
 After Raising  Exception  Ename SMITH
 After Raising  Exception  empno 
 After Raising  Exception  sal 
 
PL/SQL procedure successfully completed



Here we didn't expect the SMITH value for ename even after raising the exception

Please help to understand


Thanks
Sai Pradyumn


Re: Exception Handling For %type , %Rowtype variables [message #620432 is a reply to message #620429] Fri, 01 August 2014 06:30 Go to previous message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
saipradyumn wrote on Fri, 01 August 2014 05:33
Oracle has support the unique exceptional behavior all kinds type variables.


Have no idea what you mean by "unique exceptional behavior". Variable involved in exception has undefined value. It can be before value, can be after value or can be anything. And it can (and it sometimes does) change from release to release. What doesn't change is Oracle doesn't have any rules what variable value will be after exception. Actually it does have one rule - variable value after exception is undefined Laughing .

SY.
Previous Topic: How to write query for subpartition table
Next Topic: Rights query
Goto Forum:
  


Current Time: Thu Apr 25 11:07:17 CDT 2024