码迷,mamicode.com
首页 > 其他好文 > 详细

最简单的触发器

时间:2016-05-18 00:05:47      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:

 1 SET ANSI_NULLS ON
 2 GO
 3 SET QUOTED_IDENTIFIER ON
 4 GO
 5 --Mobile
 6 --如果存在就删除
 7 IF(OBJECT_ID(TRGINSERT_CRM_Person_Mobile,TR) IS NOT NULL)
 8    DROP TRIGGER TRGINSERT_CRM_Person_Mobile
 9 GO
10 --创建TRIGGER
11 CREATE TRIGGER  TRGINSERT_CRM_Person_Mobile
12    ON  dbo.CRM_Person 
13    AFTER INSERT
14 AS 
15   DECLARE @Mobile varchar(13)
16   SELECT  @Mobile=Inserted.PersonMobile from Inserted
17   IF(SELECT COUNT(*) FROM dbo.CRM_Person WHERE  PersonMobile =@Mobile )> 1
18 BEGIN
19     SET NOCOUNT ON;
20     PRINT(手机号码已存在!)
21     ROLLBACK TRANSACTION
22 END
23 GO
24 --========================================================================================
25 
26 --IDCardNumber
27 --如果存在就删除
28 IF(OBJECT_ID(TRGINSERT_CRM_Person_IDCardNumber,TR) IS NOT NULL)
29    DROP TRIGGER TRGINSERT_CRM_Person_IDCardNumber
30 GO
31 --创建TRIGGER
32 CREATE TRIGGER  TRGINSERT_CRM_Person_IDCardNumber
33    ON  dbo.CRM_Person 
34    AFTER INSERT
35 AS 
36   DECLARE @IDCardNumber varchar(50)
37   SELECT  @IDCardNumber=Inserted.PersonIDCardNumber from Inserted
38   IF(SELECT COUNT(*) FROM dbo.CRM_Person WHERE  PersonIDCardNumber =@IDCardNumber )> 1
39 BEGIN
40     SET NOCOUNT ON;
41     PRINT(身份证号码已存在!)
42     ROLLBACK TRANSACTION
43 END
44 GO
45 --================================================================================================
46 
47 --Email
48 --如果存在就删除
49 IF(OBJECT_ID(TRGINSERT_CRM_Person_Email,TR) IS NOT NULL)
50    DROP TRIGGER TRGINSERT_CRM_Person_Email
51 GO
52 --创建TRIGGER
53 CREATE TRIGGER  TRGINSERT_CRM_Person_Email
54    ON  dbo.CRM_Person 
55    AFTER INSERT
56 AS 
57   DECLARE @Email varchar(150)
58   SELECT  @Email=Inserted.PersonEmail from Inserted
59   IF(SELECT COUNT(*) FROM dbo.CRM_Person WHERE  PersonEmail =@Email )> 1
60 BEGIN
61     SET NOCOUNT ON;
62     PRINT(Email号码已存在!)
63     ROLLBACK TRANSACTION
64 END
65 GO
66 
67 --QQ
68 --如果存在就删除
69 IF(OBJECT_ID(TRGINSERT_CRM_Person_QQ,TR) IS NOT NULL)
70    DROP TRIGGER TRGINSERT_CRM_Person_QQ
71 GO
72 --创建TRIGGER
73 CREATE TRIGGER  TRGINSERT_CRM_Person_QQ
74    ON  dbo.CRM_Person 
75    AFTER INSERT
76 AS 
77   DECLARE @QQ varchar(50)
78   SELECT  @QQ=Inserted.PersonQQ from Inserted
79   IF(SELECT COUNT(*) FROM dbo.CRM_Person WHERE  PersonQQ =@QQ )> 1
80 BEGIN
81     SET NOCOUNT ON;
82     PRINT(QQ号码已存在!)
83     ROLLBACK TRANSACTION
84 END
85 GO

 

最简单的触发器

标签:

原文地址:http://www.cnblogs.com/linhongquan/p/5503565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!