The following two triggers were used in a Web application to indicate the availability of documents related to escrow services. When a document is uploaded to the Web site, the first trigger changes a boolean field in the database to '1' to indicate that documents are uploaded. If all documents pertaining to a particular transaction are deleted from the site, the second trigger changes the boolean field value to '0' to indicate that no documents are available.



CREATE TRIGGER dbo.AfterInsertDocAvailability ON [Atrivison].[Document] AFTER INSERT
AS
UPDATE TransactionTable
SET Documents_1_Yes_0_No = '1'
WHERE RowID = (SELECT TOP 1 TransactionNo FROM inserted)


CREATE TRIGGER dbo.noDocumentsSwitch ON [Atrivison].[Document] AFTER DELETE
AS
DECLARE @myTransactionNo int
SET @myTransactionNo = (SELECT TOP 1 TransactionNo FROM Document
WHERE TransactionNo = (SELECT TransactionNo FROM deleted))
IF @myTransactionNo = (SELECT TransactionNo FROM deleted)
BEGIN
UPDATE TransactionTable SET Documents_1_Yes_0_No = '1'
WHERE RowID = (SELECT TransactionNo FROM deleted) --this keeps it the same.
END
ELSE
BEGIN
UPDATE TransactionTable SET Documents_1_Yes_0_No = '0'
WHERE RowID = (SELECT TransactionNo FROM deleted)
END