PL/SQL Exception

PL/SQL Exception

PL/SQL Interview Questions

PL/SQL Interview Questions and Answers


PL/SQL Before Update Trigger

In PL/SQL the BEFORE UPDATE Trigger will fire before the UPDATE operation is executed.

PL/SQL Before Update Trigger Syntax

Syntax

CREATE [OR REPLACE ] TRIGGER trigger_name  
	BEFORE UPDATE ON table_name    
	[FOR EACH ROW]    
DECLARE 
   Declaration-statements 
BEGIN  
   Executable-statements 
EXCEPTION 
   Exception-handling-statements 
END;  

Where:

CREATE TRIGGER trigger_name: To create trigger with sutable name.

REPLACE OR REPLACE TRIGGER trigger_name: To replace trigger with sutable name.

BEFORE: To specifies when you want the trigger to be executed. The INSTEAD OF clause is used for creating trigger on a view.

UPDATE: To specifies DML operation.

[ON table_name]: To specifies the name of the table associated with the trigger.

[FOR EACH ROW]: To specifies a row level trigger, Otherwise the trigger will execute just once when the SQL statement is executed.

WHEN (condition): The condition on which the trigger would fire. This is valid for row level triggers.

PL/SQL Before Update Trigger Example

To show how to create an BEFORE UPDATE trigger using the CREATE TRIGGER statement, use the following syntax:

CREATE OR REPLACE TRIGGER user_before_insert
BEFORE UPDATE
   ON tblCustomerLoan
   FOR EACH ROW
DECLARE
   t_username varchar2(10);
BEGIN
   -- Find loged username performing UPDATE into table
   SELECT user INTO t_username
   FROM dual;
   -- Update create_date field to current system date
   :new.create_date := sysdate;
   -- Update created_by field to the username of the person performing the UPDATE
   :new.created_by := t_username;
END;