DBAsupport.com Forums - Powered by vBulletin
Results 1 to 3 of 3

Thread: PL/SQL Specific error messages

  1. #1
    Join Date
    May 2001
    Location
    Atlanta US
    Posts
    262

    PL/SQL Specific error messages

    Hi folks,

    Please look at the following code:

    Exception
    when others then
    sOraError := to_char(sqlcode);
    if sOraError = '-1407' then
    -- Trap Not Null error
    p_sMessage := 'Clean Vendor could not be saved as Clean
    Vendor Name is empty';
    elsif sOraError = '-1' then
    -- Trap Unique Key Constraint Error
    p_sMessage := 'Clean Vendor ' || p_sName || ' could not be
    saved as Clean Vendor Name is not unique';
    else
    p_sMessage := 'Clean Vendor ' || p_sName || ' could not be
    saved due to Oracle Error: ' || to_char(sqlcode) || ', ' || sqlerrm;
    end if


    The issue with this is, in case there are two unique key columns and if any
    of the colmn fails validation, we get OraError as -1.
    With this we will not be able to give specific messages.
    'Clean Vendor ' || p_sName || ' could not be saved as Clean Vendor
    Name is not unique';

    OR
    'Clean Vendor ' || p_sName || ' could not be saved as Clean Vendor ID
    is not unique';

    So what do you suggest to handle this scenario? Giving oracle error
    description might not make sense to the end user....

    Thanks!
    Hemant

  2. #2
    Join Date
    Dec 2000
    Location
    Ljubljana, Slovenia
    Posts
    4,439
    If I understand correctly you have two UK constraints (or one PK and one UK) and in case of the DUP_VAL_ON_INDEX exception you want to indicate to the user which one of the two constrained columns is violating the uniqueness constraint. You can do it like this:
    Code:
    ...
    exception
      when dup_val_on_index then
        begin
          select null into v_dummy from vendors where vendor_id = p_id;
          -- this p_id is allready in the database!
          p_sMessage := 'Clean Vendor ' || p_sName ||
                        ' could not be saved as Clean Vendor ID ' ||
                        'is not unique';
        exception
          when no_data_found then
            -- This p_id is not yet in the database, so VendorName
            -- must be violating the unique constraint
            p_sMessage := 'Clean Vendor ' || p_sName ||
                          ' could not be saved as Clean Vendor Name ' ||
                          'is not unique';
        end;
    ....
    end;
    Jurij Modic
    ASCII a stupid question, get a stupid ANSI
    24 hours in a day .... 24 beer in a case .... coincidence?

  3. #3
    Join Date
    May 2001
    Location
    Atlanta US
    Posts
    262
    Thanks jmodic!!!
    Hemant

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width