Pragma Exception_INIT- Doubt

Hi All
Why we are using Pragma Exception_INIT what is the purpose ?
Declare    
      e_MissingNull EXCEPTION;
     PRAGMA EXCEPTION_INIT(e_MissingNull, -1400);
BEGIN
     INSERT INTO Employees (employee_id) VALUES (NULL);
EXCEPTION
     WHEN e_MissingNull then
        DBMS_OUTPUT.put_line('ORA-1400 occurred');
END;when i give the above code i get
PL/SQL procedure successfully completed. ORA-1400 occurred
The same code when i try to give error message is ORA-1500 occurred
I get
ORA-01400: cannot insert NULL into ("XXI"."EMPLOYEES"."EMPLOYEE_ID")
ORA-06512: at line 6
why it happens?
And when i use try to get the error messgae to display Not valid instead of Zero_divide
Declare   
       I Number;
      e_sample EXCEPTION;
     PRAGMA EXCEPTION_INIT(e_sample, -5737);
BEGIN
     select 1/0 Into I From Dual ; -- I know Zero_Divide Error i thought the changing this error in my style
EXCEPTION
     WHEN e_smple then
        DBMS_OUTPUT.put_line('ORA-5737 Not valid');
END;But the Result is
ORA-01476: divisor is equal to zero
Please let me know the purpose of and usage for Pragma Exception_INIT
Thank in advance
Suresh

You must to issue:
SET SERVEROUTPUT ONto tell SQL*PLus to display DBMS_OUTPUT buffer:
SQL> Declare    
  2        e_MissingNull EXCEPTION;
  3       PRAGMA EXCEPTION_INIT(e_MissingNull, -1400);
  4  BEGIN
  5       INSERT INTO Employees (employee_id) VALUES (NULL);
  6  EXCEPTION
  7       WHEN e_MissingNull then
  8          DBMS_OUTPUT.put_line('ORA-1400 occurred');
  9  END;
10  /
PL/SQL procedure successfully completed.
SQL> SET SERVEROUTPUT ON
SQL> Declare    
  2        e_MissingNull EXCEPTION;
  3       PRAGMA EXCEPTION_INIT(e_MissingNull, -1400);
  4  BEGIN
  5       INSERT INTO Employees (employee_id) VALUES (NULL);
  6  EXCEPTION
  7       WHEN e_MissingNull then
  8          DBMS_OUTPUT.put_line('ORA-1400 occurred');
  9  END;
10  /
ORA-1400 occurred
PL/SQL procedure successfully completed.
SQL> Now about zero_divide. You are initializing exception e_sample with -5737, while zero_divide is -1476:
SQL> Declare
  2         I Number;
  3        e_sample EXCEPTION;
  4       PRAGMA EXCEPTION_INIT(e_sample, -1476);
  5  BEGIN
  6       select 1/0 Into I From Dual ; -- I know Zero_Divide Error i thought the changing this error in my style
  7  EXCEPTION
  8       WHEN e_sample then
  9          DBMS_OUTPUT.put_line('ORA-5737 Not valid');
10  END;
11  /
ORA-5737 Not valid
PL/SQL procedure successfully completed.
SQL> SY.

Similar Messages

  • Priority of pragma exception_init..

    Hi All,
    Suppose I am using pragma exception_init for NO_DATA_FOUND error,
    and the variable I am using is "test_excp" in my stored procedure, in the exception section if I am using both the user defined exception and the system defined exception (i.e. when test_excp then and when NO_DATA_FOUND then ) then which exception will the engine pick?
    This is not a business requirement but its a doubt on my part.
    Please help me understanding this concept..
    Thanks in advance!!
    Bits

    Confused yet?
    Note that there is also a -100 no data found error in Oracle but it appears to be treated as an entirely separate exception (albeit with the same message), e.g.
    Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
    SQL> DECLARE
      2     e_no_data_found EXCEPTION;
      3     PRAGMA EXCEPTION_INIT (e_no_data_found, -100);
      4  BEGIN
      5     RAISE e_no_data_found;
      6  EXCEPTION
      7     WHEN NO_DATA_FOUND THEN
      8        RAISE_APPLICATION_ERROR (-20000, 'encountered NO_DATA_FOUND exception.', TRUE);
      9     WHEN e_no_data_found THEN
    10        RAISE_APPLICATION_ERROR (-20000, 'encountered e_no_data_found exception.', TRUE);
    11  END;
    12  /
    DECLARE
    ERROR at line 1:
    ORA-20000: encountered e_no_data_found exception.
    ORA-06512: at line 10
    ORA-00100: no data found
    SQL>

  • How attend error in forms menu like pragma exception_init ?

    hello,
    I have problem with error in my menu. I created menu and I put code to my buttons in menu:for example execute_trigger('test');
    I have some forms and if I am in another forms and call my button with execute_trigger('test');
    I get error FRM-40700: No such trigger: test
    Its ok because I have trigger in another block. But how to attend this error. I put pragma execption_init but not work
    declare
    text exception;
    pragme exception_init(text,-40700);
    begin
    execute_trigger('test');
    exception
    when text then
    message('its work');
    end;
    I create code like this in menu;
    execute_trigger('test');
    if form_failure then
    message('its work');
    end if;
    end;
    and this work but I get error FRM-40700: No such trigger: test and next message is 'its work'. How to turn off message from forms?? or like this message make like pragma...
    regards

    thanks for reply.
    trigger exist but in another block.
    I check system.message-level
    thanks
    I find another solution
    I put procedure which verificate current_block and current_form and then call execute_trigger(). It's work
    Edited by: user515960 on 2010-07-18 02:43

  • Pragma exception_init

    HI,
    I want log the ORA-00942 error through exception.
    declare
    v_no number;
    v_error exception;
    pragma exception_init(v_error,-942);
    begin
    select empno into v_no from notable;
    exception
    when v_error then
    insert into log values('no table');
    end;
    when ever i execute the program ,instead of logging error in log table ..it is raising error that ' ORA-942 TABLE OR VIEW DOESN'T EXIST'. i want log this message in logtable instead of raising error
    Thanks

    Hi,
    only a short addition/illustration to/of the idea provided by SY
    -- Create Log-Table
    drop table log;
    Create table log ( msg varchar2(4000 char));
    drop table notable;
    -- P compiles without table "NOTABLE" because of the use of dynamic sql see
    -- Solomon Yakobsons post above
    create or replace
    procedure p
    as
         v_no number;
         v_stmt varchar2(2000) := 'select empno from notable';
         v_error exception;
         pragma exception_init(v_error,-942);
    begin
         EXECUTE IMMEDIATE v_stmt into v_no;
         insert into log values('j = '|| (nvl(to_char(v_no), '<<NULL>>')));
    exception
    when v_error then
         insert into log values('no table');
            -- you somehow need the following commit to make the changes in the log-table permanent;
            -- other way could be combining this method with autonomous transactions
         commit;
    when others then
         insert into log values(substr(dbms_utility.format_error_stack,1,4000));
            -- commit; /*????*/
    end;
    -- 1. table "NOTABLE" doesn't exist => "no table" in log-table
    truncate table log;
    exec p;
    select * from log;
    -- 2. table "NOTABLE" exists but is empty => "ORA-01403: no data found"
    create table notable (empno Number);
    truncate table log;
    -- writes log-message "ORA-01403: no data found" into log-table
    exec p;
    select * from log;
    -- 3. one row in "NOTABLE"; "all is well"; here "j = 1"
    -- insert first row into notable
    insert into notable (empno) values (1);
    commit; -- not needed because of the following truncate
    -- writes value of the selection into the log-table
    truncate table log;
    exec p;
    select * from log;
    -- 4. two rows in notable => "ORA-01422: exact fetch returns more than requested number of rows"
    -- insert second row
    insert into notable (empno) values (2);
    commit;
    truncate table log;
    -- writes error
    exec p;
    select * from log;So the "COMMIT" in the exception-handler may cause some trouble, if you just write without commiting here,
    a calling stored procedure might rollback your changes, which will be very misleading. The use of AUTONOMOUS TRANSACTIONS
    could get complicated too, please be careful here.
    See [url http://tkyte.blogspot.de/search?q=autonomous+transactions] Tom Kyte Blog about AUTONOMOUS TRANSACTIONS, EXCEPTION WHEN OTHERS, ... why and when autonomous transactions can help you and if you should rethink your strategy about not reraising the error. Your business logic is broken in this case, isn't it? So why carry on?
    Regards
    stratmo
    PS: User Profile for 920033     Handle:     920033
    Status Level:     Newbie
    Registered:     Mar 10, 2012
    Total Posts:     52
    Total Questions:     20 (20 unresolved)
    >
    Uuuups, seems that there is some work to do for you;-)

  • PRAGMA EXCEPTION_INIT (bulk_errors, -24381);

    PRAGMA EXCEPTION_INIT (bulk_errors, -24381);
    Can u plz explain me what is use of this statement if I declare like this. And also tell me use of the PRAGMA

    In PL/SQL, sometimes it becomes necessary to raise a predefined Oracle error
    as an exception. Depending on the error number, there are some predefined
    EXCEPTIONs that can be raised. These EXCEPTIONs are defined in package
    STANDARD and are globally available. Other errors do not have predefined
    EXCEPTIONs declared. In order to associate a predefined error number to an
    EXCEPTION, use the PRAGMA EXCEPTION_INIT. When using PRAGMA EXCEPTION_INIT,
    remember that all Oracle error numbers with exception of 1403 and 100 are
    negative.
    Once the EXCEPTION has been aliased, the EXCEPTION can then be raised.

  • Srw package throwing PRAGMA exception_init(SPECIFY_PROTOCOL, -20002)

    I've added srw.ADD_PARAMETER ( plRepParam, 'server_protocol', 'HTTP/1.1' ); to no avail. Any ideas?

    Twas muppetry on my part, I had missed http:// from my gateway param setting.

  • Bulk collection doubt

    Hi All,
    I've one doubt regarding the bulk operations using the forall statement.
    Check below eample:
    CREATE TABLE emp_temp AS SELECT * FROM employees;
    DECLARE
    TYPE empid_tab IS TABLE OF employees.employee_id%TYPE;
    emp_sr empid_tab;
    -- create an exception handler for ORA-24381
    errors NUMBER;
    dml_errors EXCEPTION;
    PRAGMA EXCEPTION_INIT(dml_errors, -24381);
    BEGIN
    SELECT employee_id BULK COLLECT INTO emp_sr FROM emp_temp
    WHERE hire_date < '30-DEC-94';
    -- add '_SR' to the job_id of the most senior employees
    FORALL i IN emp_sr.FIRST..emp_sr.LAST SAVE EXCEPTIONS
    UPDATE emp_temp SET job_id = job_id || '_SR'
    WHERE emp_sr(i) = emp_temp.employee_id;
    -- If any errors occurred during the FORALL SAVE EXCEPTIONS,
    -- a single exception is raised when the statement completes.
    EXCEPTION
    WHEN dml_errors THEN -- Now we figure out what failed and why.
    errors := SQL%BULK_EXCEPTIONS.COUNT;
    DBMS_OUTPUT.PUT_LINE('Number of statements that failed: ' || errors);
    FOR i IN 1..errors LOOP
    DBMS_OUTPUT.PUT_LINE('Error #' || i || ' occurred during '||
    'iteration #' || SQL%BULK_EXCEPTIONS(i).ERROR_INDEX);
    DBMS_OUTPUT.PUT_LINE('Error message is ' ||
    SQLERRM(-SQL%BULK_EXCEPTIONS(i).ERROR_CODE));
    END LOOP;
    END;
    Here we update using bulk collect & the exceptions for each iterations are kept separately.
    In case, I want to know whether the sql statements in each array are executed separately or all the statements are executed in bulk?
    Thanks
    Deepak

    May this will answer it...
    FORALL - documentation

  • PRAGMA forcing integral number

    The following line of code won't compile,
              PRAGMA EXCEPTION_INIT(XCPT_invalid_param, ERR_invalid_fax_code);however, this will:
              PRAGMA EXCEPTION_INIT(XCPT_invalid_param, -20001);ERR_invalid_fax_code is declared as
              ERR_invalid_fax_code           CONSTANT INTEGER := -20001;What to I do to make it accept a constant var, instead of an integral number?

    The second argument to EXCEPTION_INIT must be a literal.
    I don't know why, that's just the way it is.
    See:
    http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96525/pcmus.htm#1001616

  • Differnce between exception_init and init_exception

    I have declared an usernamed exception and wanted to call...
    I came to know that these can be called by using two of the below -
    Pragma Exception_init
    Pragma Init_Exception
    What is the differnce between[b] exception_init and init_exception ? And where they can be used ?
    awating for your rresponse ..!

    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0:
    SQL>declare
      2     my_exception exception;
      3     pragma exception_init(my_exception, -20101);
      4  begin
      5     raise my_exception;
      6  exception
      7     when my_exception then
      8        dbms_output.put_line(sqlerrm);
      9  end;
    10  /
    ORA-20101:
    PL/SQL procedure successfully completed.
    SQL>declare
      2     my_exception exception;
      3     pragma init_exception(my_exception, -20101);
      4  begin
      5     raise my_exception;
      6  exception
      7     when my_exception then
      8        dbms_output.put_line(sqlerrm);
      9  end;
    10  /
       pragma init_exception(my_exception, -20101);
    ERROR at line 3:
    ORA-06550: line 3, column 11:
    PLS-00127: Pragma INIT_EXCEPTION is not a supported pragma

  • PRAGMA EXCEPTION

    Morning,
    Would some one please review this code below? I am trying to capture the oracle error table or view does not exist -00942 but it does not seem to want to work. What am I typing wrong?? It throws the error instead of it being handle within the exception section. This table is not available when I run it in 9i.
    I simplified the select statement.
    declare
    table_view_not_exist EXCEPTION;
    PRAGMA EXCEPTION_INIT(table_view_not_exist, -00942);
    begin
    select end_time from dba_optstat_operations;
    EXCEPTION
    WHEN table_view_not_exist
    THEN
    DBMS_OUTPUT.PUT_LINE('PLEASE RUN ANALYZE MANUALLY');
    end;
    Any help in this matter would be appreciated.
    Thanks in advance and have a great day.
    al

    The code cannot compile if the table isn't present.
    Try this:
    DECLARE
         table_view_not_exist EXCEPTION;
         PRAGMA EXCEPTION_INIT(table_view_not_exist, -942);
            v_end_time DATE;
    BEGIN
         EXECUTE IMMEDIATE 'SELECT end_time FROM dba_optstat_operations'
            INTO v_end_time;
            DBMS_OUTPUT.PUT_LINE('End date: ' || TO_CHAR(v_end_time,'YYYY-MM-DD HH24:MI:SS'));
    EXCEPTION
         WHEN table_view_not_exist THEN
              dbms_output.put_line('Please run analyze manually');
    END;

  • Doubt in erorhandling package

    /* Formatted by PL/Formatter v3.1.2.1 on 2000/12/02 15:32 */
    DROP TABLE errlog;
    CREATE TABLE errlog (
    errcode INTEGER,
    errmsg VARCHAR2(4000),
    created_on DATE,
    created_by VARCHAR2(100)
    CREATE OR REPLACE PACKAGE err
    IS
    c_table CONSTANT PLS_INTEGER := 1; -- Default
    c_file CONSTANT PLS_INTEGER := 2;
    c_screen CONSTANT PLS_INTEGER := 3;
    PROCEDURE handle (
    errcode IN PLS_INTEGER := NULL,
    errmsg IN VARCHAR2 := NULL,
    logerr IN BOOLEAN := TRUE,
    reraise IN BOOLEAN := FALSE
    PROCEDURE raise (
    errcode IN PLS_INTEGER := NULL,
    errmsg IN VARCHAR2 := NULL
    PROCEDURE log (
    errcode IN PLS_INTEGER := NULL,
    errmsg IN VARCHAR2 := NULL
    PROCEDURE logto (
    target IN PLS_INTEGER,
    dir IN VARCHAR2 := NULL,
    file IN VARCHAR2 := NULL
    FUNCTION logging_to
    RETURN PLS_INTEGER;
    END;
    CREATE OR REPLACE PACKAGE BODY err
    IS
    g_target PLS_INTEGER := c_table;
    g_file VARCHAR2 (2000) := 'err.log';
    g_dir VARCHAR2 (2000) := NULL;
    PROCEDURE handle (
    errcode IN PLS_INTEGER := NULL,
    errmsg IN VARCHAR2 := NULL,
    logerr IN BOOLEAN := TRUE,
    reraise IN BOOLEAN := FALSE
    IS
    BEGIN
    IF logerr
    THEN
    log (errcode, errmsg);
    END IF;
    IF reraise
    THEN
    err.raise (errcode, errmsg);
    END IF;
    END;
    PROCEDURE raise (
    errcode IN PLS_INTEGER := NULL,
    errmsg IN VARCHAR2 := NULL
    IS
    l_errcode PLS_INTEGER := NVL (errcode, SQLCODE);
    l_errmsg VARCHAR2(1000) := NVL (errmsg, SQLERRM);
    BEGIN
    IF l_errcode BETWEEN -20999 AND -20000
    THEN
    raise_application_error (l_errcode, l_errmsg);
    /* Use positive error numbers -- lots to choose from! */
    ELSIF l_errcode > 0
    AND l_errcode NOT IN (1, 100)
    THEN
    raise_application_error (-20000, l_errcode || '-' || l_errmsg);
    /* Can't EXCEPTION_INIT -1403 */
    ELSIF l_errcode IN (100, -1403)
    THEN
    RAISE NO_DATA_FOUND;
    /* Re-raise any other exception. */
    ELSIF l_errcode != 0
    THEN
    PLVdyn.plsql ('DECLARE myexc EXCEPTION; ' ||
    ' PRAGMA EXCEPTION_INIT (myexc, ' ||
    TO_CHAR (l_errcode) ||
    ');' ||
    'BEGIN RAISE myexc; END;'
    END IF;
    END;
    PROCEDURE log (
    errcode IN PLS_INTEGER := NULL,
    errmsg IN VARCHAR2 := NULL
    IS
    PRAGMA AUTONOMOUS_TRANSACTION;
    l_sqlcode pls_integer := NVL (errcode, SQLCODE);
    l_sqlerrm VARCHAR2(1000) := NVL (errmsg, SQLERRM);
    BEGIN
    IF g_target = c_table
    THEN
    INSERT INTO errlog
    (errcode, errmsg, created_on, created_by)
    VALUES (
    l_sqlcode,
    l_sqlerrm,
    SYSDATE,
    USER
    ELSIF g_target = c_file
    THEN
    DECLARE
    fid UTL_FILE.file_type;
    BEGIN
    fid := UTL_FILE.fopen (g_dir, g_file, 'A');
    UTL_FILE.put_line (fid,
    'Error log by ' || USER || ' at ' ||
    TO_CHAR (SYSDATE, 'mm/dd/yyyy')
    UTL_FILE.put_line (fid, NVL (errmsg, SQLERRM));
    UTL_FILE.fclose (fid);
    EXCEPTION
    WHEN OTHERS
    THEN
    UTL_FILE.fclose (fid);
    END;
    ELSIF g_target = c_screen
    THEN
    DBMS_OUTPUT.put_line ('Error log by ' || USER || ' at ' ||
    TO_CHAR (SYSDATE, 'mm/dd/yyyy')
    DBMS_OUTPUT.put_line (NVL (errmsg, SQLERRM));
    END IF;
    COMMIT;
    EXCEPTION
    WHEN OTHERS
    THEN
    ROLLBACK;
    END;
    PROCEDURE logto (
    target IN PLS_INTEGER,
    dir IN VARCHAR2 := NULL,
    file IN VARCHAR2 := NULL
    IS
    BEGIN
    g_target := target;
    g_file := file;
    g_dir := dir;
    END;
    FUNCTION logging_to
    RETURN PLS_INTEGER
    IS
    BEGIN
    RETURN g_target;
    END;
    END;
    in this package
    PLVdyn.plsql ('DECLARE myexc EXCEPTION; ' ||
    ' PRAGMA EXCEPTION_INIT (myexc, ' ||
    TO_CHAR (l_errcode) ||
    ');' ||
    'BEGIN RAISE myexc; END;'
    is used.actuallyi got this code for oracle best practices.but i am not getting the code for PLVdyn code.is it built package or private package.i want the code for PLYdyn.plsql code

    user10447332 wrote:
    in this package
    PLVdyn.plsql ('DECLARE myexc EXCEPTION; ' ||
    ' PRAGMA EXCEPTION_INIT (myexc, ' ||
    TO_CHAR (l_errcode) ||
    ');' ||
    'BEGIN RAISE myexc; END;'
    is used.actuallyi got this code for oracle best practices.but i am not getting the code for PLVdyn code.is it built package or private package.i want the code for PLYdyn.plsql codeOk, so yesterday you took some 3rd party code for error handling but didn't know what to do with it.
    Now it looks like you've taken some more code from somewhere else but you haven't got the full thing.
    PLVdyn is not a built in Oracle package, so wherever you obtained the rest of the code would be a good place to start looking for the source for it.
    Of course, producing dyanamic PL/SQL is not really a good thing to do and makes applications a nightmare to debug and maintain. If you don't understand the basics then you really shouldn't be trying something so complex. Is this for homework like we thought yesterday or is it (scary thought) coding for a business contract you have obtained without the necessary skills?

  • Trapping in APForm Trig via PRAGMA EXCEPTION

    hi,
    I can trap 06052 errors in report 6i (After pform trig)
    but not -01843,-01847 and a few others relating to date format input mask errors. Anyone know why?
    Thanks.
    N.
    Here's how I do it: (in after pform trig)
    date_invalid EXCEPTION;
    PRAGMA EXCEPTION_INIT (date_invalid, -01843);
    BEGIN
    EXCEPTION
    when date_invalid THEN srw.message('Date must in Format ''DD-MON-YYYY'' !');
    RETURN ( FALSE );
    END;

    .

  • Funct return two values

    hi,
    can i make a function return more than one value ?
    thanks.
    n/

    Nicholas said he wanted to use this in Forms 6i. Well, 9i Forms does not support stored procedures that return object values, so I doubt very much that6i does.
    I suggest using a procedure with two OUT parameters:
    PROCEDURE get_default_qty (
    p_supp_id IN edtrad.orcrsupp.supp_id%TYPE
    , p_item_no IN edtrad.orcrstit.stit_item_id%TYPE
    , p_ord_id IN edtrad.orcrodet.odet_order_id%TYPE
    , p_qty OUT NUMBER
    , p_val2 OUT VARCHAR2)
    IS
    qty_not_found EXCEPTION;
    PRAGMA EXCEPTION_INIT ( qty_not_found, -20004 );
    v_qty edtrad.orcrdlvd.dlvd_qty_delivrd%TYPE;
    v_val2 VARCHAR2(9);
    CURSOR qty
    IS
    SELECT vd.dlvd_qty_delivrd, "value_two"
    FROM edtrad.orcrdlvd vd,
    edtrad.orcrodet dt
    WHERE vd.dlvd_supp_id = p_supp_id
    AND vd.dlvd_order_id = p_ord_id
    AND dt.odet_item_id = p_item_no
    AND vd.dlvd_order_id = dt.odet_order_id
    AND vd.dlvd_ord_line_no = dt.odet_line_no ;
    BEGIN
    OPEN qty;
    FETCH qty INTO v_qty, v_val2;
    IF ( qty%NOTFOUND ) THEN
    CLOSE qty;
    RAISE qty_not_found;
    END IF;
    CLOSE qty;
    p _qty := v_qty ;
    p _val2 := v_val2;
    END get_default_qty ;
    Notes
    (1) You must fetch a cursor into a matching set of variables (or define a %ROWTYPE).
    (2) set_default_qty is a bad name for this method. set implies value changing. This method doesn't alter anything, it simply retrieves data. Consequently, it should be called get_default_qty.
    Cheers, APC

  • Locking in forms

    i have a doubt in that in my application, users can select some sequential numbers from a table, i am using the following statement to lock a record once the user has selected a record and not to allow the other user to see this particular record and to let him select the next sequential record. The statement i am using is as follows:
    SELECT STATUS INTO V_TEMP FROM TABL_SERAIL_NO
    WHERE ser_no = R_RCS.RC_SC_ID AND STATUS = 'AA' AND EXPIRY_DATE >= TRUNC(SYSDATE) FOR UPDATE OF STATUS NOWAIT;
    In the above statement, the clause 'FOR UPDATE OF STATUS NOWAIT' is used to lock the record. Now, the problem is that when the user exits the front-end session without a commit, the locked record remains locked for sometime, i guess 5-10 mins. I think, my query is clear to u. Now, can u please help me in understanding as to exactly what is happening as it is very urgent.

    Hi,
    You might want to try the following ON-LOCK trigger:
    declare
    dummy char(1); -- select .. into variable
    row_free boolean not null := false; -- if the record is free, exit the loop and lock it
    row_locked exception; -- if the record is locked, ask the user what to do
    pragma exception_init(row_locked, -54); -- catch "ORA-00054: RESOURCE BUSY AND .." error
    no_tries number not null := 0; -- number of tries
    begin
    loop
    begin
    select '1' -- try to lock the current record
    into dummy
    from emp
    where rowid = :emp.rowid
    for update nowait;
    row_free := true;
    exit; -- if success, exit the loop
    exception
    when row_locked then -- handle error ORA-00054, increment no_tries
    no_tries := no_tries + 1;
    end;
    -- change the alert message
    set_alert_property('al_lock', alert_message_text,
    'Record locked (attempt '&#0124; &#0124;no_tries&#0124; &#0124;'). Continue trying?');
    exit when show_alert('al_lock') = alert_button2; -- if user clicks no, exit the loop
    end loop;
    if row_free then -- record is not locked, let's lock it
    lock_record;
    else -- we're out of the loop, but the record is not ours
    message('Error: record was not reserved for update or delete');
    raise form_trigger_failure;
    end if;
    end;Don't forget to create an alert named AL_LOCK, with two buttons ('Yes' and 'No').
    Hope this helps,
    Pedro.

  • Help, question about "select ... for update nowait"

    There is a proc code. In the beginning of the code, I used a SQL "select ... for update nowait" in order to prevent from another proc executing at the same time. When the case happens, "-54, ORA-00054: resource busy and acquire with NOWAIT specified" will be printed in the screen.
    But there is a question: I need to print sth to indicate "another proc is running". I used "if (sqlca.sqlcode == -54)" as precondition, such as:
    if (sqlca.sqlcode == -54) {
    printf("There is another proc running.\n");
    However, this line will not be printed. I doubt that the code quits directly when using "select ... for update nowait" so as not to set value (-54) to sqlca.sqlcode.
    So, could you suggest whether there is another way that I can use to print "There is another proc running" when another proc is running?
    Thx a lot for your kindly reply.

    Yes, that link. Scroll down a bit and you will see:
    The calling application gets a PL/SQL exception, which it can process using the error-reporting functions SQLCODE and SQLERRM in an OTHERS handler. Also, it can use the pragma EXCEPTION_INIT to map specific error numbers returned by raise_application_error to exceptions of its own, as the following Pro*C example shows:
    EXEC SQL EXECUTE
    /* Execute embedded PL/SQL block using host
    variables v_emp_id and v_amount, which were
    assigned values in the host environment. */
    DECLARE
    null_salary EXCEPTION;
    /* Map error number returned by raise_application_error
    to user-defined exception. */
    PRAGMA EXCEPTION_INIT(null_salary, -20101);
    BEGIN
    raise_salary(:v_emp_id, :v_amount);
    EXCEPTION
    WHEN null_salary THEN
    INSERT INTO emp_audit VALUES (:v_emp_id, ...);
    END;
    END-EXEC;
    This technique allows the calling application to handle error conditions in specific exception handlers.

Maybe you are looking for