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

Home
SQL Server / T-SQL Tutorial
1.Query
2.Insert Delete Update
3.Table
4.Table Join
5.Data Types
6.Set Operations
7.Constraints
8.Subquery
9.Aggregate Functions
10.Date Functions
11.Math Functions
12.String Functions
13.Data Convert Functions
14.Analytical Functions
15.Sequence Indentity
16.View
17.Index
18.Cursor
19.Database
20.Transact SQL
21.Procedure Function
22.Trigger
23.Transaction
24.XML
25.System Functions
26.System Settings
27.System Tables Views
28.User Role
29.CLR
SQL Server / T-SQL Tutorial » Trigger » Trigger 
22.1.11.Check record matching in a trigger
2>    CREATE TABLE Activity(
3>       ActivityID         int   IDENTITY (11)    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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.