I want to create a trigger to fill a field according to acondition as below description
For example:
I have a table for all insps that have attachments named: “inspectionAttachments” and I have another table “inspections” that contains all insps and I want to fill for example column/field named “shop” in the inspections table to define which insps that have attachments or not
So, I want to fill the shop field with Yes if at least this insp has one attachment and fill this field with No if there is no attachement
How I can write the trigger to make what I want
Thanks for any help ?
Best,
Lubna
Best Answer
The following solution assumes rows will be added to the
inspections
table first (with a default value ofNO
in theShop
column. It is also assumed that only one attachment can exists for a giveninspectionid
Two triggers have been defined on the
attachments
table.The
insert
trigger on theattachments
table will update theinspections
table withYES
in theShop
column to reflect the added attachment.The
delete
trigger on theattachments
table will update theinspections
table withNO
in theShop
column to reflect the deleted attachment.