Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

PL/SQL block is terminated by the end keyword.

The structure of a typical PL/SQL block is shown in the following listing: declare <declarations section> begin <executable commands> exception <exception handling> end;

declare pi constant NUMBER(9,7) := 3.1415927; radius INTEGER(5); area NUMBER(14,2); begin radius := 3; area := pi*power(radius,2); insert into AREAS values (radius, area); end; /

Customizing Error Conditions: create or replace trigger BOOKSHELF_BEF_DEL before delete on BOOKSHELF declare weekend_error EXCEPTION; not_library_user EXCEPTION; begin if TO_CHAR(SysDate,'DY') = 'SAT' or TO_CHAR(SysDate,'DY') = 'SUN' THEN RAISE weekend_error; end if; if SUBSTR(User,1,3) <> 'LIB' THEN RAISE not_library_user; End if; EXCEPTION WHEN weekend_error THEN

RAISE_APPLICATION_ERROR (-20001, 'Deletions not allowed on weekends'); WHEN not_library_user THEN RAISE_APPLICATION_ERROR (-20002, 'Deletions only allowed by Library users'); end; /
The final portion of the trigger body tells the trigger how to handle the exceptions. It begins with the keyword exception, followed by a when clause for each of the exceptions. Each of the exceptions in this trigger calls the RAISE_APPLICATION_ERROR procedure, which takes two input parameters: the error number (which must be between 20001 and 20999), and the error message to be displayed. In this example, two different error messages are defined, one for each of the defined exceptions.

Calling Procedures Within Triggers


Rather than creating a large block of code within a trigger body, you can save the code as a stored procedure and call the procedure from within the trigger, by using the call command. For example, if you create an INSERT_BOOKSHELF_AUDIT_DUP procedure that inserts rows into BOOKSHELF_ AUDIT_DUP, you can call it from a trigger on the BOOKSHELF_AUDIT table, as shown in the following listing:

create or replace trigger BOOKSHELF_AFT_INS_ROW after insert on BOOKSHELF_AUDIT for each row begin call INSERT_BOOKSHELF_AUDIT_DUP(:new.Title, :new.Publisher, :new.CategoryName, :new.Old_Rating, :new.New_Rating, :new.Audit_Date); end; /

You might also like