Home » SQL & PL/SQL » SQL & PL/SQL » Procedure not running and not even throwing any error (Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production PL/SQL Release 11.2.0.1.0 - Production CORE 11.2.0.1.0 Production TNS for IBM/AIX RISC System/6000: Version 11.2.0.1.0 - Pr)
Procedure not running and not even throwing any error [message #600791] Tue, 12 November 2013 10:29 Go to next message
gupta27
Messages: 31
Registered: August 2013
Location: Delhi
Member
I have a procedure which is not working properly. It goes into success status without updating any row into main table using the cursor. It happens sometimes only and not always.

below is the code of the procedure:

CREATE OR REPLACE procedure scott.emp_increm_proc as
    rows_read     number := 0;
    rows_inserted number := 0;
    rows_updated  number := 0;
    cursor        c1 is select * from emp_increm;
begin
    for cur_emp in c1
    loop
      rows_read := rows_read + 1;
      begin
      insert into employee (        EMP_NUM,                 NAME_1,                        NAME_2,
                                   NAME_3,                   NAME_4,                        CITY,
                                   REGION,                   POSTAL_CD,                     CNTRY_CD,
                                   CITY_CD,                  CNTY_CD,                       CNTY_DESC,
                                   STREET_ADDR,              PO_BOX,                        PO_BOX_POSTAL_CD
                             )           
                 values ( cur_emp.emp_NUM,        cur_emp.NAME_1,               cur_emp.NAME_2,
                          cur_emp.NAME_3,          cur_emp.NAME_4,               cur_emp.CITY,
                          cur_emp.REGION,          cur_emp.POSTAL_CD,            cur_emp.CNTRY_CD,
                          cur_emp.CITY_CD,         cur_emp.CNTY_CD,              cur_emp.CNTY_DESC,
                          cur_emp.STREET_ADDR,     cur_emp.PO_BOX,               cur_emp.PO_BOX_POSTAL_CD
                           ) ;
      rows_inserted := rows_inserted + 1;
      exception
      when dup_val_on_index
      then
      rows_updated := rows_updated + 1;
      update employee c
         set c.NAME_1               = cur_emp.NAME_1,               
             c.NAME_2               = cur_emp.NAME_2,
             c.NAME_3               = cur_emp.NAME_3, 
             c.NAME_4               = cur_emp.NAME_4,
             c.CITY                 = cur_emp.CITY,               
             c.REGION               = cur_emp.REGION,
             c.POSTAL_CD            = cur_emp.POSTAL_CD,   
             c.CNTRY_CD             = cur_emp.CNTRY_CD,
             c.CITY_CD              = cur_emp.CITY_CD,
             c.CNTY_CD              = cur_emp.CNTY_CD,
             c.CNTY_DESC            = cur_emp.CNTY_DESC,          
             c.STREET_ADDR          = cur_emp.STREET_ADDR, 
             c.PO_BOX               = cur_emp.PO_BOX,
             c.PO_BOX_POSTAL_CD     = cur_emp.PO_BOX_POSTAL_CD  
       where c.emp_num = cur_emp.emp_num;
      end;
    commit;
    end loop;
    dbms_output.put_line('Read     ' || rows_read);
    dbms_output.put_line('Inserted ' || rows_inserted);
    dbms_output.put_line('Updated  ' || rows_updated);
end emp_increm_proc;


Is it because I have not explicitly mentioned the schema name for the tables used in the procedure.
Note: There are no other tables in the database (and schema)with these names.

How can I handle these scenarios in exception, so that it would throw failure status when read = 0(i.e when procedure is not able to read anything from the cursor table)
Re: Procedure not running and not even throwing any error [message #600792 is a reply to message #600791] Tue, 12 November 2013 10:43 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
I don't see anyway that the schema name could be relevant, it knows which schema it's using when it compiles.

you could use raise_application_error when rows_read = 0.
Re: Procedure not running and not even throwing any error [message #600793 is a reply to message #600791] Tue, 12 November 2013 10:50 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Not running? Throw it out. All that code can be replaceed with a single MERGE statement.

SY.
Re: Procedure not running and not even throwing any error [message #600794 is a reply to message #600792] Tue, 12 November 2013 10:54 Go to previous messageGo to next message
gupta27
Messages: 31
Registered: August 2013
Location: Delhi
Member
Can you please tell me how to use raise_application_error in the code as I am kinda new to PL/SQL.
Re: Procedure not running and not even throwing any error [message #600795 is a reply to message #600794] Tue, 12 November 2013 11:03 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
http://docs.oracle.com/cd/E16655_01/server.121/e17209/statements_9016.htm#SQLRF01606
Re: Procedure not running and not even throwing any error [message #600804 is a reply to message #600794] Tue, 12 November 2013 11:57 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
never do in PL/SQL that which can be done in plain SQL
Re: Procedure not running and not even throwing any error [message #600805 is a reply to message #600795] Tue, 12 November 2013 11:58 Go to previous message
gupta27
Messages: 31
Registered: August 2013
Location: Delhi
Member
Thanks all for your quick help.
Previous Topic: FORALL Stops when First Errors Occures
Next Topic: update
Goto Forum:
  


Current Time: Thu Apr 18 22:26:23 CDT 2024