Exception block insert

I am just wondering if it's except-able to put and insert in the exception block? What I would like is if date2 is null kick off an error. if there are no records where date2 is null then go ahead and insert the new record. Is this common practice or should I use sql%rowcount or put count in to variable and check if greater then 0?
create or replace
PROCEDURE X_INSERT
(P_ID NUMBER,
P_DATE1 DATE,
P_DATE2 DATE
AS
a NUMBER;
BEGIN
select ID into a from X where ID = P_ID
AND DATE_2 IS NULL ;
if sql%found then
raise_application_error(-20000, 'record exists');
END IF;
exception
when no_data_found then
insert into X values (P_ID,P_DATE1, P_DATE2);
END X_INSERT;

>
I am just wondering if it's except-able to put and insert in the exception block?
>
No - I do not think that approach is acceptable and would not recommend it.
You need any exception LOGGING (inserts) to be permanent even if the transaction is rolled back.
One way to accomplish this is to write a LOG procedure (e.g. SP_LOG) that uses PRAGMA AUTONOMOUS_TRANSACTION so that the logging will occur even if the original transaction is rolled back. You can have overloaded versions of SP_LOG that take different sets of parameters.
The code in the exception block would call the LOG procedure to do the logging and would pass it any information relevant to the exception and where it was caused. A sample generic EXCEPTION block might be
  EXCEPTION
   WHEN OTHERS THEN
   /* Build error string to help debug problem */
   v_error_text := ('Exception :'
                    || SQLERRM
                    || ' Parse error :'
                    || TO_CHAR (SQLCODE)
                    || ' at position '
                    || TO_CHAR (DBMS_SQL.LAST_ERROR_POSITION)
                    || ' SQL Text used :'
   /* Log this error in a table - for researching problems. */
   SP_LOG ( v_error_text );
  END main_sp;There would normally be other parameters to the log procedure to identify the stored procedure and package name where the exception was raised, the exception error stack and so on.
The exception block might also need to re-raise the exception rather than make it disappear so that the caller is aware of the exception that occured.
In your case there is no real exception. You just want to log information.
So instead of raising an exception you would just modify your code to call the logging procedure.
if sql%found then
   SP_LOG('Cannot INSERT record into myTable - record exists');
END IF;

Similar Messages

  • Capturing error in Exceptions Block in a PLSQL Procedure

    Hi,
    I am creating a procedure where i need to update a table with some constraints.
    i need to update atleast a million records with data from another table.
    but here is the catch while updating million records, there may be some records which wont be vaild because of the constraints and cannot be updated and hence will give an error.
    but in my procedure i want to write an exception block where it captures the error, ignores the error and keep coninuing the procedure and update all the remaining records instaed of getting hanged at the point of erorr.
    How can i do this.
    I know i can disable the constraints in the table.
    but i want the constraints enabled, so that the errors are trapped and skipped and only the records that are valid are updated.
    Can seomone help me write this exception block which does this function.
    Thanks,
    Philip.

    Hi,
    I used the exception bloack as u said.
    i have a sample of 20 records and i know the 11th record is not valid and should be inserted in a different way from the rest 19 records.
    so i ran the same query with the exceptions block.
    but what happened, until 10th record everything was fine.
    on the 11th record the execution went into the exceptions block and executed whatever was there in the exception block, but then the script just exited after exception block, it did not go back to fetch the remaining records from 12th unitl 20th.
    How can i fix this.
    Philip.

  • Ref cursor in exception block

    Hi all.. I have a stored procedure that needs to return a records to Crystal Report.
    I'm using an outpur parameter of type cursor.
    In the proc, I am selecting the data from a table based on the input criteria and inserting into a GTT table,
    if so, retrieve and store in the table and generate and throw output to CR.
    if it is not returning any data i should raise exception and throw the related message to CR.
    But in my proc even though it wont insert data into table b going inot next statements where it should not go.
    create or replace procedure a(a1 varchar2,ref_cur out pkg1.cru)
    as
    cnt number;
    begin
    insert into b
    (select * from a where a.col=a1);
    commit;
    open ref_cur for
    select a.col from a;
    EXCEPTION
    WHEN OTHERS THEN
    RAISE_APPLICATION_ERROR (-20001, SQLERRM);
    WHEN NO_DATA_FOUND THEN
         open ref_cur for
    select 'No Data Found' col from dual;
    end;
    1.please suggest wherer to raise exception when "select * from a where a.col=a1" doesn't return any value.
    2. Can ref cursor be used in exception block.
    if i use condition like
    after the insert statement
    select count(*) into cnt from b;
    if cnt=0 then
    raise NO_DATA_FOUND;
    EXCEPTION
    WHEN OTHERS THEN
    RAISE_APPLICATION_ERROR (-20001, SQLERRM);
    WHEN NO_DATA_FOUND THEN
         open ref_cur for
    select 'No Data Found' col from dual;
    please suggest any other way to handle
    Thanks in advance

    First and foremost some fundamental things:
    EXCEPTION
    WHEN OTHERS THEN
    RAISE_APPLICATION_ERROR (-20001, SQLERRM);
    WHEN NO_DATA_FOUND THEN
    open ref_cur for
    select 'No Data Found' col from dual;
    end;1. You can not declare WHEN OTHERS before any other EXCEPTION. You can not compile this. Here you have declared WHEN OTHERS first then NO_DATA_FOUND and it is erroneous.
    2. This is not the way you want to handle the WHEN OTHERS exception.
    WHEN OTHERS_
    http://tkyte.blogspot.com/2008/06/when-others-then-null-redux.html
    http://tkyte.blogspot.com/2007/03/dreaded-others-then-null-strikes-again.html
    http://download.oracle.com/docs/cd/E11882_01/appdev.112/e17126/toc.htm

  • Exception block in a procedure

    I have this exception block in my procedure, it use to work nicely (write log in UNIX, but since we upgrade to 10G is not working anymore.
    I ask my DBA, he said that I do have the right previlages to this
    Directory /home/connman/student';
    v_out_path VARCHAR2 (40) := '/home/connman/student';
    What is wrong? I will appreciate any help on this..
    PROCEDURE studlife_tbl_ins_rep_room_ind (
    p_studlife_repeat_err_code OUT VARCHAR2,
    p_ora_err_code OUT NUMBER,
    p_ora_err_msg OUT VARCHAR2
    IS
    v_out_path VARCHAR2 (40) := '/home/connman/student';
    v_out_file VARCHAR2 (40)
    := 'cc_student_life_pkg'
    || '_'
    || TO_CHAR (SYSDATE, 'YYYYMMDDHH');
    v_file_handle UTL_FILE.file_type;
    v_pidm NUMBER;
    v_count NUMBER;
    e_insert_zero EXCEPTION;
    CODE (GO HERE)
    p_studlife_repeat_err_code := '0';
    UTL_FILE.put_line (v_file_handle, 'Successful Completion.');
    SELECT COUNT (repeat_pidm)
    INTO v_count
    FROM SZSLIFE_REPEAT_ROOM;
    --- WHERE szslife_rm_asg_term_code_eff = p_stulife_term_code;
    IF v_count = 0
    THEN
    RAISE e_insert_zero;
    ELSE
    UTL_FILE.put_line (v_file_handle,'Number of Records Inserted' || v_count);
    END IF;
    EXCEPTION
    WHEN e_insert_zero
    THEN
    ROLLBACK;
    p_studlife_repeat_err_code :=
    'CC_ERROR_MSG: Insert Procedure. ZERO Rows Returned in INSERT statement for term '
    || '.';
    p_ora_err_msg := SUBSTR (SQLERRM, 1, 2000);
    p_ora_err_code := SQLCODE;
    UTL_FILE.put_line (v_file_handle,
    p_studlife_repeat_err_code
    || 'sqlerrm: '
    || p_ora_err_msg
    || ' / sqlcode: '
    || SQLCODE
    WHEN OTHERS
    THEN
    ROLLBACK;
    p_studlife_repeat_err_code :=
    'CC_ERROR_MSG: Error on INSERT statement for term '
    || p_studlife_repeat_err_code
    || '.';
    p_ora_err_msg := SUBSTR (SQLERRM, 1, 2000);
    p_ora_err_code := SQLCODE;
    END studlife_tbl_ins_rep_room_ind;

    I don't think my DBA knows what is going on, I did read and here is the entire precedure, as I said use to work in Oracle 9.2. since we upgrade to 10G it is
    not working, I am not getting an error, It is not wring anything to the directory
    '/home/connman/student'; my dba said that I have the right priveliges to that directory...
    PROCEDURE studlife_tbl_ins_rep_room_ind (
    p_studlife_repeat_err_code OUT VARCHAR2,
    p_ora_err_code OUT NUMBER,
    p_ora_err_msg OUT VARCHAR2
    IS
    v_out_path VARCHAR2 (40) := '/home/connman/student';
    v_out_file VARCHAR2 (40)
    := 'cc_student_life_pkg'
    || '_'
    || TO_CHAR (SYSDATE, 'YYYYMMDDHH');
    v_file_handle UTL_FILE.file_type;
    v_pidm NUMBER;
    v_count NUMBER;
    e_insert_zero EXCEPTION;
    BEGIN
    UTL_FILE.fclose_all;
    v_file_handle := UTL_FILE.fopen(v_out_path, v_out_file, 'a');
    UTL_FILE.put_line (v_file_handle,
    CHR (10) || TO_CHAR (SYSDATE, 'DD-MON-YYYY HH:MI:SS')
    UTL_FILE.put_line (v_file_handle, 'studlife_tbl_ins_rep_room_ind .');
    INSERT INTO SZSLIFE_REPEAT_ROOM
    repeat_pidm ,
    repeat_term_code ,
    repaat_bldg_code ,
    repeat_desc ,
    repeat_room_number ,
    repeat_indicator
    SELECT
    DISTINCT
    temp_pidm,
    temp_term_code,
    temp_bldg_code ,
    temp_desc ,
    temp_room_number ,
    DECODE (lag(temp_room_number)
    over (PARTITION BY temp_bldg_code, temp_room_number ORDER BY temp_term_code) , NULL,'N', temp_room_number,'Y',NULL)
    FROM
    SZSLIFE_TEMP_TBL
    ---WHERE temp_pidm = 1862
    ORDER BY
    temp_term_code;
    COMMIT;
    p_studlife_repeat_err_code := '0';
    UTL_FILE.put_line (v_file_handle, 'Successful Completion.');
    SELECT COUNT (repeat_pidm)
    INTO v_count
    FROM SZSLIFE_REPEAT_ROOM;
    --- WHERE szslife_rm_asg_term_code_eff = p_stulife_term_code;
    IF v_count = 0
    THEN
    RAISE e_insert_zero;
    ELSE
    UTL_FILE.put_line (v_file_handle,'Number of Records Inserted' || v_count);
    END IF;
    EXCEPTION
    WHEN e_insert_zero
    THEN
    ROLLBACK;
    p_studlife_repeat_err_code :=
    'CC_ERROR_MSG: Insert Procedure. ZERO Rows Returned in INSERT statement for term '
    || '.';
    p_ora_err_msg := SUBSTR (SQLERRM, 1, 2000);
    p_ora_err_code := SQLCODE;
    UTL_FILE.put_line (v_file_handle,
    p_studlife_repeat_err_code
    || 'sqlerrm: '
    || p_ora_err_msg
    || ' / sqlcode: '
    || SQLCODE
    WHEN OTHERS
    THEN
    ROLLBACK;
    p_studlife_repeat_err_code :=
    'CC_ERROR_MSG: Error on INSERT statement for term '
    || p_studlife_repeat_err_code
    || '.';
    p_ora_err_msg := SUBSTR (SQLERRM, 1, 2000);
    p_ora_err_code := SQLCODE;
    END studlife_tbl_ins_rep_room_ind;

  • Block insertion module button

    Hi all,
    use Adobe Acrobat X Pro. With some PDF files I am having problems inserting buttons. When I do the Insert, adobe acrobat x pro crashes. I already tried disabling all plugins, applying all available updates. the problem persists.
    the same file, even with other previous version, except block with Adobe Acrobat 7 Pro.
    because Adobe Acrobat 7 Pro I can insert buttons in the PDF file, whereas with Adopbe Acroba 9 and X PRO can't because you block?
    can you help?
    Thanks to all.

    ,i need it because if the user had loggedout then by clicking back button he can again go to the previous login page which should not be............................Hmm.. I can do that after logging in to this forum, but if I login a second time I get a confirmation as to whether I want to logout and login again. Not that everything about the forums is to be held up as a shining example of best practices, but maybe that's the way to go?
    db

  • Save all details error in Exception Block of BizTalk

    In orchestration A,I make a Exception Block ,created a System.Exception object and saved Exception Error in a string and checking it in admin console.
    In my orchestarion, I got amount value from a element Amount, used a custom xslt and in that custom xslt, I used a C# function for desired calculation for the value of amount.
    Now when,input string of amount is not in a correct format then a error is saved regarding mapping  as follows:
    Transformation failed for Map A.
    But the actual error is as follows:
    input string was not in a correct format.
    How can I get this error in  exception handler block?
    Prakash

    Hi Prakash,
    Like you have mentioned that you are using Custom XSLT, my advice will be that you can raise an exception from within an XSLT template, based on the value.
    <xsl:message terminate="yes">Custom error text</xsl:message>
    This will cause the XSLT engine to stop processing immediately, and raise an exception.   This exception, including the custom error text contained within the message segment,
    can be caught in the BizTalk Orchestration engine by explicitly catching an exception of type 
    Microsoft.XLANGS.BaseTypes.TransformationFailureException.
    Refer: Biz Talk : How To : Throw Custom Exception in Map
    For step-by-step explanation on this refer: Flowing clear error messages from transforms
    Rachit
    Please mark as answer or vote as helpful if my reply does

  • How to handle the plsql error occuring in the exception block

    We know how to handle exceptins which occur in BEGIN block.
    But am unable to catch the exception in the exception block. Am writing an erroeneous code so that the control will go to exception block and there is also one plsql error, but am unable to handle that error, it's returning the error to the calling environment.
    DECLARE
    cnt NUMBER(5):=0;
    BEGIN
    select 'debalina' INTO cnt from dual;
    DBMS_OUTPUT.PUT_LINE(to_char(cnt));
    EXCEPTION
    WHEN invalid_number THEN
    DBMS_OUTPUT.PUT_LINE('error has occured inside begin block');
    cnt:='deba';
    WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('error has occured inside begin block');
    END;
    please suggest me how to catch this exception?

    Hi,
    DECLARE
    cnt NUMBER(5):=0;
    BEGIN
    select 'debalina' INTO cnt from dual;
    DBMS_OUTPUT.PUT_LINE(to_char(cnt));
    EXCEPTION
    WHEN invalid_number THEN
    DBMS_OUTPUT.PUT_LINE('error has occured inside begin block');
    cnt:='deba';
    WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('error has occured inside begin block');
    END;
    First of all your namee exception which you have posted i.e invalid_number itself is wrong.
    You need to use named exception VALUE_ERROR for catching the exception in the main block.
    SQL> DECLARE
      2  cnt NUMBER(5):=0;
      3  BEGIN
      4  select 'debalina' INTO cnt from dual;
      5  DBMS_OUTPUT.PUT_LINE(to_char(cnt));
      6  EXCEPTION
      7  WHEN Invalid_number THEN
      8  DBMS_OUTPUT.PUT_LINE('error has occured inside main block');
      9  end;
    10  /
    DECLARE
    ERROR at line 1:
    ORA-06502: PL/SQL: numeric or value error: character to number conversion error
    ORA-06512: at line 4
    SQL>  DECLARE
      2   cnt NUMBER(5):=0;
      3  BEGIN
      4  select 'debalina' INTO cnt from dual;
      5  DBMS_OUTPUT.PUT_LINE(to_char(cnt));
      6  EXCEPTION
      7  WHEN VALUE_ERROR THEN
      8  DBMS_OUTPUT.PUT_LINE('error has occured inside main block');
      9  end;
    10  /
    error has occured inside main block
    PL/SQL procedure successfully completed.Your doubt regarding catching the exception in exception block, you can execute as below, by nesting a Begin block inside the exception block itself.
    SQL> DECLARE
      2  cnt NUMBER(35):=0;
      3  BEGIN
      4  select 'debalina' INTO cnt from dual;
      5  DBMS_OUTPUT.PUT_LINE(to_char(cnt));
      6  EXCEPTION
      7  WHEN Value_error THEN
      8  DBMS_OUTPUT.PUT_LINE('error has occured inside main block');
      9  Begin
    10  cnt:='deba';
    11  Exception
    12  WHEN OTHERS THEN
    13  DBMS_OUTPUT.PUT_LINE('error has occured inside exception block');
    14  End;
    15  END;
    16  /
    error has occured inside main block
    error has occured inside exception block
    PL/SQL procedure successfully completed.Hope your doubt is clear.
    Twinkle

  • Informix 7.3 - Oracle 8i migration: Generate Exception Blocks not offered

    I have installed OMWB 2.0.2 and plugin for Infromix Dynamic Server 7.3.
    There is no offered checkbox "Generate Exception Blocks" in "Parse Options" panel for customization of parsing stored procedures in Source Model.
    (But it was normally offered and working in previous OMWB 1.4.1 wersion).
    How I could coerce OMWB2.0.2 to generate BEGIN-EXCEPTION-END blocks around every SELECT statement in stored procedures ?
    Many thanks
    Vladimir Kubanka (alias Bare Foot)
    [email protected] , [email protected]

    Vladimir,
    Generate Exception Blocks parse option is available on the T/SQL to PL/SQL parsers (Sybase and Microsoft SQLServer), I suppose you could put in an enhancement request if this functionality was important to yourself and other users.
    Turloch

  • DBMS_OUTPUT in exception block

    Hi:
    I have a user defined exception and I am trying to use DBMS_OUTPUT.PUT_LINE in the exception block. Even though I run the code in sqlplus with serverputput on, the dbms_output is not displayed. Why?
    Here is an example:
    DECLARE
    salary_too_high EXCEPTION;
    current_salary NUMBER := 20000;
    max_salary NUMBER := 10000;
    erroneous_salary NUMBER;
    BEGIN
    BEGIN ---------- sub-block begins
    IF current_salary > max_salary THEN
    RAISE salary_too_high; -- raise the exception
    END IF;
    EXCEPTION
    WHEN salary_too_high THEN
    -- first step in handling the error
    DBMS_OUTPUT.PUT_LINE('Salary ' || erroneous_salary || ' is out of range.');
    DBMS_OUTPUT.PUT_LINE('Maximum salary is ' || max_salary || '.');
    END;
    Please help.
    Thanks.
    Bruce

    Is this the complete code you have, or this is just part of the whole code?
    I tried to run it, and it's perfectly OK.
    SQL> set serverout on size 100000
    SQL> DECLARE
      2  salary_too_high EXCEPTION;
      3  current_salary NUMBER := 20000;
      4  max_salary NUMBER := 10000;
      5  erroneous_salary NUMBER;
      5  BEGIN
      6     begin
      7   
      8    if current_salary > max_salary then
      9      RAISE salary_too_high; -- raise the exception
    10    END IF;
    11    EXCEPTION
    12    WHEN salary_too_high THEN
    13    -- first step in handling the error
    14    DBMS_OUTPUT.PUT_LINE('Salary ' || erroneous_salary || ' is out of range.');
    15 dbms_output.put_line('Maximum salary is ' || max_salary || '.');
    16 end;
    17 end;
      18 /
    Salary  is out of range.
    Maximum salary is 10000.
    PL/SQL procedure successfully completed.

  • DBMS_XMLGEN --Number of active cursors in increased in Exception block

    Hi ,
    We have a custom code that use DBMS_XMLGEN,the issue is when any exception happens the Number os active cusrors increse and when it happens in a lopp we hit 1000ORA-01000: maximum open cursors exceeded.
    Although I close the context in the exception Block.
    Below is sample code where I could replicate this:
    Step1.Create a table
    CREATE TABLE EMP(EMP_NO NUMBER,EMPNAME VARCHAR2(100));
    Step2:
    Create  a Procedure:
    CREATE OR REPLACE PROCEDURE TEST_XMLGEN
    AS
    l_clob CLOB;
      l_ctx         dbms_xmlgen.ctxhandle;
      p_sql VARCHAR2(4000):='SELECT EMP_NO,,EMPNAME FROM EMP';--syntax error on purpose ,so that exception happens
      l_cursor_count NUMBER;
    BEGIN
    SELECT VALUE
               INTO l_cursor_count
               FROM v$mystat a, v$statname b
              WHERE     a.statistic# = b.statistic#
                    AND b.name = 'opened cursors current';
            DBMS_OUTPUT.PUT_LINE ('open cursors place00' || l_cursor_count);
    l_ctx := dbms_xmlgen.newcontext (p_sql);
    SELECT VALUE
               INTO l_cursor_count
               FROM v$mystat a, v$statname b
              WHERE     a.statistic# = b.statistic#
                    AND b.name = 'opened cursors current';
             DBMS_OUTPUT.PUT_LINE('open cursors place01' || l_cursor_count);
    dbms_xmlgen.getxml (l_ctx,l_clob);
    SELECT VALUE
               INTO l_cursor_count
               FROM v$mystat a, v$statname b
              WHERE     a.statistic# = b.statistic#
                    AND b.name = 'opened cursors current';
             DBMS_OUTPUT.PUT_LINE ('open cursors place02' || l_cursor_count);
    DBMS_OUTPUT.PUT_LINE('l_clob:'||l_clob);
    dbms_xmlgen.closecontext (l_ctx);
    SELECT VALUE
               INTO l_cursor_count
               FROM v$mystat a, v$statname b
              WHERE     a.statistic# = b.statistic#
                    AND b.name = 'opened cursors current';
             DBMS_OUTPUT.PUT_LINE ('open cursors place03' || l_cursor_count);
    EXCEPTION
    WHEN OTHERS
    THEN
    SELECT VALUE
               INTO l_cursor_count
               FROM v$mystat a, v$statname b
              WHERE     a.statistic# = b.statistic#
                    AND b.name = 'opened cursors current';
             DBMS_OUTPUT.PUT_LINE ('open cursors place04' || l_cursor_count);
    dbms_xmlgen.closecontext (l_ctx);
    SELECT VALUE
               INTO l_cursor_count
               FROM v$mystat a, v$statname b
              WHERE     a.statistic# = b.statistic#
                    AND b.name = 'opened cursors current';
             DBMS_OUTPUT.PUT_LINE ('open cursors place05' || l_cursor_count);
    END;
    Step3: Execute above Procedure
    If you look at the number of active cursor it is increased in exception block,I suspect this is what is causing issue with my program too.Any suggestions to solve this issue.
    Thanks
    Shefali

    Did you mean this test:
    declare
      ts timestamp;
      type tp_strings is table of varchar2(32767) index by pls_integer;
      t_strings tp_strings;
      t_tmp xmltype;
      t_nd dbms_xmldom.domnode;
      t_nl dbms_xmldom.domnodelist;
      t_clob clob;
    begin
      t_clob := '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
             || '<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="5000" uniqueCount="5000">';
      for i in 1 .. 5000
      loop
        t_clob := t_clob || to_clob( '<si><t>A' || to_char( i ) || '</t></si>' );
      end loop;
      t_clob := t_clob || '</sst>';
      t_tmp := xmltype( t_clob );
      t_strings.delete;
      ts := systimestamp;
          select str
          bulk collect into t_strings
          from xmltable( xmlnamespaces( default 'http://schemas.openxmlformats.org/spreadsheetml/2006/main' )
                       , '/sst/si' passing t_tmp
                       columns str clob path 't'
      dbms_output.put_line( systimestamp - ts );
      dbms_output.put_line( t_strings.count() || ' ' || t_strings( t_strings.first )  || ' ' || t_strings( t_strings.last ) );
      t_strings.delete;
      ts := systimestamp;
          select /*+ NO_XML_QUERY_REWRITE */ str
          bulk collect into t_strings
          from xmltable( xmlnamespaces( default 'http://schemas.openxmlformats.org/spreadsheetml/2006/main' )
                       , '/sst/si' passing t_tmp
                       columns str clob path 't'
      dbms_output.put_line( systimestamp - ts );
      dbms_output.put_line( t_strings.count() || ' ' || t_strings( t_strings.first )  || ' ' || t_strings( t_strings.last ) );
      t_strings.delete;
      ts := systimestamp;
          t_nd := dbms_xmldom.makenode( dbms_xmldom.getdocumentelement( dbms_xmldom.newdomdocument( t_tmp ) ) );
          t_nl := dbms_xslprocessor.selectnodes( t_nd, '/sst/si/t/text()', 'xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"' );
          for i in 0 .. dbms_xmldom.getlength( t_nl ) - 1
          loop
            t_strings( i ) := dbms_xmldom.getnodevalue( dbms_xmldom.item( t_nl, i ) );
          end loop;
      dbms_output.put_line( systimestamp - ts );
      dbms_output.put_line( t_strings.count() || ' ' || t_strings( t_strings.first )  || ' ' || t_strings( t_strings.last ) );
    end;  

  • RAISE_APPLICATION_ERROR works only in exception block

    Hi,
    I am facing a strange issue. My RAISE_APPLICATION_ERROR wroks only in EXCEPTION block and not in BEGIN block. Please have a look at below description
    I have a procedure that has IF Block as shown below.
    BEGIN
    IF TO_DATE('31-DEC-99', 'DD-MON-RR') > TO_DATE('11-MAR-12', 'DD-MON-RR') THEN
    RAISE_APPLICATION_ERROR(-20001, 'some message');
    END IF;
    END;
    -- RAISE_APPLICATION_ERROR is not working in the above code.
    BEGIN
    IF TO_DATE('31-DEC-99', 'DD-MON-RR') > TO_DATE('11-MAR-12', 'DD-MON-RR') THEN
    RAISE EX_ABC
    END IF;
    EXCEPTION
    WHEN EX_ABC THEN
    RAISE_APPLICATION_ERROR(-20001, 'some message');
    END;
    --Here the exception is raised and it works fine.
    BACKDROP:
    I have made a trigger that does date validations. It calls procedure for all the validations. And the above shown code is a part of the validations that the procedure does.
    Please help.
    Edited by: Ishan on Mar 14, 2012 1:28 PM
    Edited by: Ishan on Mar 14, 2012 1:31 PM

    Ishan wrote:
    I have myself checked and tested with all these conditions. Tried all to_date formats and dbms_output to be sure of the values but nothing seems to work.
    It works in a sample code. But when implemented in the procedure it is ignoring the RAISE_APPLICATION_ERROR.
    Condition is correct. Below is the logic because of which I can say it's correct
    If 1=1 then
    raise_application_error (-20001, 'ABC');
    end;
    this ignores the raise_application_error.
    However if I modify the code with exception block, WITHOUT CHANGING THE CONDITION, iti works fine
    if 1=1 then
    raise ex_ab
    exception
    raise_application_error (-20001, 'ABC');
    end;
    Now the exception is raised.
    NOTE: EVen if I am using 1=1 condition in my code, it is still ignoring the RAISE_APPLICATION error but it will raise the same in he exception block.
    Not sure how is this possible.Are you saying the below code is giveing different result in your db?
    BEGIN
    IF 1=1 THEN
    RAISE_APPLICATION_ERROR(-20001, 'some message');
    END IF;
    END;

  • Why is User defined exception block not reaching

    I want to catch exception if wrong column name is there in a select statement.
    In this example i am useing static query but in real time it will be a dynamic query and it may be possible that a particualre column has been deleted from that table so an error will be returned that we need to catch and display custom message to the user. But control is not going to exception block.
    type_id1 column is not there in table1
    what is that i am missing here?
    declare
    vcTypeID varchar2(10);
    invalid_COLUMNs EXCEPTION;
    pragma exception_init(invalid_COLUMNs,-06550);
    begin
    select to_char(type_id1) into vcTypeID from table1 WHERE ROWNUM=1;
    dbms_output.put_line(vcTypeID);
    exception
    when invalid_COLUMNs then
    dbms_output.put_line('def');
    when others then
    dbms_output.put_line('others');
    end;
    Edited by: user13065317 on Jun 1, 2012 12:47 AM

    Hi,
    Why are you trying to catch 6550? I'd rather try with 904 - invalid identifier
      1  DECLARE
      2  e EXCEPTION;
      3  PRAGMA EXCEPTION_INIT(e, -904);
      4  v VARCHAR2(128);
      5  BEGIN
      6   EXECUTE IMMEDIATE 'SELECT TO_CHAR(nonexisting) FROM dual' INTO v;
      7  EXCEPTION
      8   WHEN e THEN dbms_output.put_line('Invalid identifier');
      9* END;
    SQL> /
    Invalid identifier
    PL/SQL procedure successfully completed.
    SQL>Lukasz

  • To handle exception in insert query

    Hi ,
    I am trying it insert certain records from another table A to temp table B, while doing insert i am having exception of unique constraint in table B how can i check which record is throwing exception while inserting in table A
    for eg:
    insert into A
    select * from B;
    i need to handle exception in which record of B i am getting unique constraint error

    Hi,
    If you add an error logging clause (with keywords LOG ERRORS), then you can insert a row into another table for each row that can't be inserted according to your INSERT statement.
    To query table A to see which ids already exist in table B, you can say
    SELECT  *
    FROM    a
    WHERE   a_id  IN (
                          SELECT  b_id
                          FROM    b
    where b.b_id is the unique key in table b, and a.a_id is the value you're truing to insert into that column.
    If you just want to skip the insert when a matching row already exists, then use MERGE instead of INSERT.

  • Isn't this Exception block redundant?

    DB Version: 10.2.0.4
    Found this on procedure code.
    begin
              select nvl(user_id,'NONE')into v_userid from user_info where user_id = p_userid;              
    exception
    when no_data_found then
    RAISE_APPLICATION_ERROR (-20002, 'No Such User');
    end;NO_DATA_FOUND occurs when SELECT query retuns NULL, not when the SELECT returns
    no rows selectedSince NVL is handling NULLs in the SELECT query, the above exception block is never going to be used. Right?

    user872043 wrote:
    DB Version: 10.2.0.4
    Found this on procedure code.
    begin
              select nvl(user_id,'NONE')into v_userid from user_info where user_id = p_userid;              
    exception
    when no_data_found then
    RAISE_APPLICATION_ERROR (-20002, 'No Such User');
    end;NO_DATA_FOUND occurs when SELECT query retuns NULL, not when the SELECT returns
    no rows selected
    No, it's just the opposite. NO_DATA_FOUND is raised when no rows are returned. It doesn't matter what's in that row: all NULLs is okay.
    Since NVL is handling NULLs in the SELECT query, the above exception block is never going to be used. Right?That's easy for you to test. Pass a value of p_userid that you know does not exist.
    The EXCEPTION block is redundant in the sense that Oracle will raise an error anyway. It would be NO_DATA_FOUND, not 'No Such User', but the line number in the error message would point to the SELECT statement, not the RAISE_APPLICATION_ERROR statement.

  • Scope of the exception block

    I have a meanful exception, under where the exception may occure I have a excptionhandling block. I dont know if i can use the variabled in the block where the exception occured.
    thx in advance
    Andre

    thx i dont know if i can also do a select command from the exception block:
    the followig code couldnt be compiled:
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
    BEGIN
    SELECT ***** INTO *** FROM ***** WHERE ***=SELECT MIN(***) FROM **** WHERE ***** > **** AND *** like **** AND **** like *** and
    END,
    the error message was: found select where.. expected
    thx
    Andre

Maybe you are looking for