CREATE TRIGGER Command

Example   See Also

Creates a Delete, Insert, or Update trigger for a table.

Syntax

CREATE TRIGGER ON TableName
  FOR DELETE | INSERT | UPDATE AS lExpression

Arguments

TableName

Specifies the table in the current database for which a trigger is created.

FOR DELETE | INSERT | UPDATE

Specifies the type of trigger Visual FoxPro creates.

If a trigger of the type you specify already exists and SET SAFETY is ON, Visual FoxPro asks you if you'd like to overwrite the existing trigger. If SET SAFETY is OFF, the existing trigger is automatically overwritten.

AS lExpression

Specifies the logical expression evaluated when the trigger occurs. lExpression can be a user-defined function or a stored procedure that returns a logical value. Stored procedures are created for a table with MODIFY PROCEDURE.

A user-defined function or a stored procedure can use AERROR( ) to determine the name of the table for which the trigger occurred and the trigger type.

If lExpression evaluates to true (.T.), the command or event that caused the trigger to occur is executed.

If lExpression evaluates to false (.F.), the command or event that caused the trigger to occur isn't executed. If an ON ERROR procedure is in effect, the ON ERROR procedure is executed instead of the command or event. If an ON ERROR procedure is not in effect, the command or event isn't executed and Visual FoxPro generates an error message.

Remarks

Use CREATE TRIGGER to trap for events that cause records in a table to be deleted, added, or changed. Delete, Insert, or Update triggers can be created only for a table that has been added to a database. Use CREATE DATABASE to create a database, and ADD TABLE to add a table to a database.

The following lists describe the events that cause a Delete, Insert, or Update trigger to occur.

Delete Trigger

Insert Trigger

Update Trigger

The following rules apply to triggers created with CREATE TRIGGER:

If table buffering is in effect, the Update trigger occurs when TABLEUPDATE( ) is issued and each buffered record is updated in the table.