Use the CREATE TRIGGER command to create temporal triggers that fire at a specified frequency.
CREATE [ OR REPLACE ] TRIGGER trigger_name
GROUP group_name
[ DEBUG { TRUE | FALSE } ]
[ ENABLED { TRUE | FALSE } ]
PRIORITY integer
[ COMMENT 'comment_string'
EVERY integer { HOURS | MINUTES | SECONDS }
[ EVALUATE SELECT_cmd BIND AS variable_name ]
[ WHEN condition ]
[ DECLARE variable_declaration ]
BEGIN
trigger_action
END;
If there is a possibility that a trigger already exists with the same name as the one that you want to create, use the optional OR REPLACE keywords. If the trigger exists, it is replaced by the one that you are creating. If the trigger does not exist, a new one is created.
The trigger_name value must be unique within the ObjectServer and comply with the ObjectServer naming conventions.
The group_name value can be any trigger group already created by using the CREATE TRIGGER GROUP command.
If DEBUG is set to TRUE, debugging information is sent to the ObjectServer message log, if the message level is set to debug.
If ENABLED is set to TRUE, the trigger fires when the associated incident occurs. Otherwise, the trigger does not fire when the incident occurs.
The PRIORITY of a trigger determines the order in which the ObjectServer fires triggers when more than one trigger is associated with the same incident. The priority can be in the range of 1 to 20. The lower the number, the higher the priority, so a trigger with a priority of 2 is fired before a trigger with a priority of 3. If more than one trigger of the same priority is fired because of the same incident, the order in which these triggers fire is undetermined.
Use the optional COMMENT keyword to add a comment (comment_string) for the trigger.
Within a temporal trigger, you must specify how often the trigger will fire. Specify an integer value in seconds (the default unit of time), minutes, or hours.
Use the optional EVALUATE clause to build a temporary result set from a single SELECT statement to be processed in the trigger_action. The SELECT statement cannot contain an ORDER BY clause.
An EVALUATE clause can mostly be replaced with a FOR EACH ROW clause. Use an EVALUATE clause only when a GROUP BY clause is required.
Use the optional WHEN clause to test for a particular condition before the trigger action runs. If the condition is not met, the trigger action does not run.
You can optionally declare local trigger variables for use in the body of the trigger. These variables are declared and used in the same way as procedure variables. However, trigger variables are static, so they maintain their value between the times when the trigger runs.
The following temporal trigger deletes all clear rows (Severity = 0) from the alerts.status table that have not been modified in the last two minutes.
create trigger DeleteClears
group my_triggers
priority 1
every 60 seconds
begin
delete from alerts.status where Severity = 0
and StateChange < (getdate - 120);
end;