The CREATE TRIGGER statement defines a trigger for a base table (see Table).
<create_trigger_statement> ::= CREATE TRIGGER <trigger_name> FOR
<table_name>
AFTER <trigger_event,..>
EXECUTE (<routine>) [WHENEVER <search_condition>
]
<trigger_event> :: INSERT | UPDATE [(<column_list>)] | DELETE
<column_list> ::= <column_name> |
<column_list>,<column_name>
trigger_name, table_name, search_condition, routine, column_name
The trigger ensures that the hotel number in the roomtable is also changed when a hotel number is changed in the hotel table.
CREATE TRIGGER hotel_update FOR hotel AFTER UPDATE EXECUTE (
TRY
IF NEW.hno <> OLD.hno
THEN UPDATE travel.room SET hno = :NEW.hno WHERE hno =
:OLD.hno;
CATCH
IF $rc <> 100
THEN STOP ($rc, 'unexpected error');
)
A trigger is a special type of database procedure that is assigned to a base table. This database procedure cannot be executed explicitly with the CALL statement, but rather automatically by SAP DB when defined events (trigger events) for the table occur.
SAP DB provides a language (special SQL syntax that has been extended to include variables, control structures, and troubleshooting measures) that can be used to define database procedures and triggers.
The specified synonym name must identify an existing base table of the current user.
The trigger event defines what triggers the trigger. The trigger is always invoked if the triggering event has been processed correctly.
INSERT: the INSERT trigger event causes the trigger to be executed for each row inserted in the table.
UPDATE: the UPDATE event causes the trigger to be executed for each modification made to a row in the table. If a column list is specified, the trigger is only called if one of the columns in the column list was modified.
DELETE: the DELETE trigger event causes the trigger to be executed for every row deleted from the table.
A maximum of one trigger can be defined for each trigger event in each table.
Each INSERT trigger implicitly has a corresponding variable NEW.<column_name> for each column in the table. When the trigger is executed, this variable has the value of the corresponding column in the inserted row. It is only permissible to specify NEW for SQL statements specified in routine_sql_statements. Specifying NEW for the other statements leads to an error.
Each UPDATE trigger implicitly has a corresponding variable NEW.<column name> and OLD.<column name> for each column in the table. When the trigger is executed, the OLD.<column_name> variable has the value of the corresponding column in front of and NEW.<column_name> after the change in the row. Specifying NEW and OLD is optional.
Each DELETE trigger implicitly has a corresponding variable OLD.<column_name> for each column in the table. When the trigger is executed, this variable has the value of the corresponding column in the deleted row. It is only permissible to specify OLD for SQL statements specified in routine_sql_statements. Specifying OLD for the other statements leads to an error.
:NEW and
:OLD must always be
used with a colon in SQL statements that are used in triggers and that belong
to the
routine_sql_statements
(For example:
UPDATE travel.room SET hno = :NEW.hno WHERE hno = :OLD.hno).
NEW and
OLD must always be
used without a colon in SQL statements that are used in triggers and
that do not belong to the
routine_sql_statements
(For example:
IF NEW.hno <> OLD.hno).
See also:
If the trigger is terminated by STOP with an error number not equal to zero, the entire SQL statement that triggered the trigger fails.
The SUBTRANS statement is not allowed in a trigger.
If a WHENEVER statement is specified, the trigger is only executed if the search condition is fulfilled. The condition must not contain a subquery nor any set functions.