Check record matching in a trigger : Trigger « Trigger « SQL Server / T-SQL Tutorial






2>    CREATE TABLE Activity(
3>       ActivityID         int   IDENTITY (1, 1)    NOT NULL,
4>       ActivityType       int                      NOT NULL,
5>       ActivityDate       datetime                 NOT NULL,
6>       ActivityComplete   bit NOT                  NULL
7>    )
8>    CREATE TABLE ActivityFootball(
9>       ActivityID        int       NOT NULL,
10>       InstantReplay     bit       NOT NULL,
11>       FlagTackle        bit       NOT NULL,
12>       TwoPointPlay      bit       NOT NULL
13>    )
14>    GO
1>
2>    CREATE TABLE ActivitySoftball(
3>       ActivityID        int       NOT NULL,
4>       NoOfRefs          tinyint   NOT NULL,
5>       DiamondSize       tinyint   NOT NULL,
6>       StealingAllowed   bit       NOT NULL
7>    )
8>    GO
1>
2>
3>    CREATE TRIGGER FootballIsExclusiveActivity ON ActivityFootball
4>       FOR INSERT, UPDATE
5>    AS
6>       IF EXISTS(
7>           SELECT 'True'
8>           FROM Inserted i
9>           LEFT JOIN Activity a
10>              ON i.ActivityID = a.ActivityID
11>           WHERE a.ActivityID IS NULL
12>          )
13>       BEGIN
14>          RAISERROR('Football item Must Have Corresponding Activity',16,1)
15>          ROLLBACK TRAN
16>       END
17>       IF EXISTS(
18>           SELECT 'True'
19>           FROM Inserted i
20>           LEFT JOIN ActivitySoftball asb ON i.ActivityID = asb.ActivityID
21>           WHERE asb.ActivityID IS NOT NULL
22>          )
23>       BEGIN
24>          RAISERROR('Matching Softball Record Exists.',16,1)
25>          ROLLBACK TRAN
26>       END
27>     GO
1>
2> drop TRIGGER FootballIsExclusiveActivity ;
3> GO
1>
2> drop table Activity;
3> drop table ActivityFootball;
4> drop table ActivitySoftball;
5> GO








22.1.Trigger
22.1.1.The syntax of the CREATE TRIGGER statement
22.1.2.exits if the price column has not been updated.
22.1.3.Define variables in a trigger
22.1.4.Update table in a trigger
22.1.5.Check @@ROWCOUNT in a trigger
22.1.6.Rollback transaction in a trigger
22.1.7.RAISERROR in trigger
22.1.8.Disable a trigger
22.1.9.Enable a trigger
22.1.10.Check business logic in a trigger
22.1.11.Check record matching in a trigger
22.1.12.Table for INSTEAD OF Trigger for Logical Deletes
22.1.13.INSTEAD OF INSERT trigger for a table
22.1.14.Trigger Scripts for Cascading DELETEs