Triggers in Firebird
Triggers in Firebird
General
Triggers are self-contained routines (special Stored Procedures), which are
connected to a table or view
A trigger is automatically executed ("fired") when a record is inserted, updated or
deleted from a table/view
Triggers are never called directly but only through one of the commands
INSERT, UPDATE or DELETE
Triggers are written in Stored Procedure and Trigger Language and can use
Exceptions
Advantages
Automatic enforcement of conditions and limitations for the contents
Reduced maintenance for the applications
You can implement a modification logging mechanism for tables
Generators can be called automatically and their values assigned to fields
Automatic notification of applications by calling of Events
Automatic upper-casing of fields to enable case-insensitive searching
General Syntax
CREATE [OR ALTER] TRIGGER name FOR tablename
[ACTIVE | INACTIVE]
{BEFORE | AFTER} {INSERT | UPDATE | DELETE}
[OR {INSERT | UPDATE | DELETE}
[OR {INSERT | UPDATE | DELETE}]]
[POSITION number]
AS <trigger_body>
ACTIVE/INACTIVE: A trigger can be deactivated, e.g. for development. Can
also be used with ALTER TRIGGER.
BEFORE/AFTER: Tells the system if the trigger must be fired before or after the
Insert/Update/Delete operation
POSITION: Trigger calling order. Triggers with low numbers are fired first.
Triggers with the same position number are fired in an undefined order. The
position defaults to 0.
trigger_body: Same as body in a Stored Procedure
Context Variables
Variable Name Contents
OLD.field Content of field before insert/update/delete
NEW.field New field content for an insert or update operation
INSERTING True if the trigger is called for an INSERT operation
UPDATING True if the trigger is called for an UPDATE operation
DELETING True if the trigger is called for a DELETE operation
Examples
Insert a Generator value for a newly inserted record
CREATE TRIGGER Create_Log_ID FOR Logs
BEFORE INSERT
AS BEGIN
NEW.ID = GEN_ID (LogIdGenerator, 1);
END;
Suppose you have a table of employees and want to be able to search for employee last
name, independent of case:
Now include a new field LASTNAME_UPPER which will contain the uppercased
lastname. You will also need a collation so that the UPPER() function can work at all.
Now you need a trigger which will fill the LASTNAME_UPPER field at every INSERT
or UPDATE operation. There is no need for your application to fill
LASTNAME_UPPER.
Next you need an index on the LASTNAME_UPPER field so that searching is fast:
Now you can search for employees using a normal SELECT and by using
LASTNAME_UPPER instead of LASTNAME. Remember to uppercase the term you are
searching for:
Advantages
Parts of code only implemented once: client applications get smaller and less
complicated to implement and mantain.
Easier Maintenance: Client applications don't need to be recompiled and
redistributed
Increased performance because of reduced network traffic
(In and Out parameters of a Stored Procedure are used like variables)
SET TERM
Every command in a script must be terminated by a semi-colon, the procedure itself, too.
To distinguish the semi-colons in the procedure from the terminating semi-colon, there
must be another terminator for the end of the procedure. This is done with SET TERM:
SET TERM !! ;
CREATE PROCEDURE x AS BEGIN ... END !!
SET TERM ; !!
The first "SET TERM" replaces the terminator semi-colon with the terminator double-
exclamation. The procedure declaration contains the usual semi-colons after each
command. The procedure itself is terminated by the "new" terminator !!. After that, the
terminator symbol is set back to a semi-colon.
Exceptions
Create: CREATE EXCEPTION name “message“
Alter: ALTER EXCEPTION name “message“
Drop: DROP EXCEPTION name
Examples
/* --- Returning a single value –----------------------------------- */
CREATE PROCEDURE Mul (a INTEGER, b INTEGER)
RETURNS (Result INTEGER)
AS BEGIN
Result = a * b;
END