Unsure how to phrase plsql exception

35 views Asked by At

I want to write an exception for the Check column. If 'Ok' or 'Not Ok' have not been entered i want to raise an application error. How would i go about writing this?

2

There are 2 answers

0
Dan Armour On

Something like the following?

ALTER TABLE [TableName]
ADD CONSTRAINT [CheckName] CHECK (Check IN ('Ok', 'Not Ok'));

This will raise a DB error if any other value is input into the column,

More information here: https://www.w3schools.com/sql/sql_check.asp

0
APC On

This is the simplest way to raise a bespoke PL/SQL exception:

 begin
    if p_check not in ('Ok', 'Not Ok') then
       raise_application_error(-20999, 
            'Invalid value for CHECK '|| p_check);
    end if;
    ....

Numbers for user-defined exceptions must be in the range -20999 to -20000.

Incidentally, you describe check as a "column". PL/SQL exceptions are for variables and parameters. If what you mean really is validating a table column you need a check constraint:

alter table whatever
    add constraint col23_ck 
         check ( col23 in ('Ok', 'Not Ok'));