Condividi tramite


CREATE TRIGGER Command

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

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

Parameters

  • 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

  • DELETE is issued.
  • A record is marked for deletion from the Table Menu in a Browse window or Edit window.
  • Note that issuing ZAP does not cause the Delete trigger to occur.

Insert Trigger

  • APPEND FROM is issued.
  • APPEND FROM ARRAY is issued.
  • APPEND BLANK is issued.
  • A record is appended from the Table Menu in a Browse window or Edit window.
  • IMPORT is issued.
  • INSERT – SQL is issued.
  • RECALL is issued.
  • A record is recalled from the Table Menu in a Browse window or Edit window.

Update Trigger

  • GATHER is issued.
  • REPLACE is issued.
  • REPLACE FROM ARRAY is issued.
  • UPDATE – SQL is issued.
  • Other event that causes a record to be modified, such as when a Form changes the contents of a field.

The following rules apply to triggers created with CREATE TRIGGER:

  • INSERT cannot be issued for a table with a trigger. However, INSERT – SQL can be used.
  • Issuing PACK doesn't cause any triggers to occur.
  • Issuing ZAP doesn't cause a Delete trigger to occur.
  • No trigger occurs if you update a record marked for deletion.
  • A trigger may not occur immediately, depending on the current buffering mode:

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

Example

The following example creates an Update trigger which prevents values greater than 50 from being entered in the maxordamt field in the customer table. An error message is generated when the first REPLACE command is executed because the value for the maxordamt field is greater than 50. The second REPLACE command does not generate an error because the value for the maxordamt field is less than or equal to 50.

CLOSE DATABASES

OPEN DATABASE (HOME(2) + 'data\testdata')
USE customer  && Open customer table

* Set trigger on maxordamt field to fail with values <= 50
CREATE TRIGGER ON customer FOR UPDATE AS maxordamt <= 50

ON ERROR  && Restore the system error handler

WAIT WINDOW "Press a key to test trigger with value of 60"+CHR(13);
 +"When you get the error message, press Ignore."
REPLACE maxordamt WITH 60    && Displays an error message
? maxordamt

WAIT WINDOW "Press a key to test with value of 50."
REPLACE maxordamt WITH 50    && Value is accepted
? maxordamt
DELETE TRIGGER ON customer FOR UPDATE  && Remove the trigger

See Also

ADD TABLE | AERROR( ) | CREATE DATABASE | DELETE TRIGGER | DISPLAY DATABASE | LIST DATABASE | OPEN DATABASE