sqlserver觸發器的使用

在sqlserver2008中因爲服務器網絡設置須要,部署了兩個結構相同的數據庫,並要求對這兩個數據庫中其中任何一個庫作任何更改均要同步到另外一個庫中sql

因此就在兩個庫分別建立了觸發器,以便同步數據庫

------------------------------------------------------------------------------服務器

如庫1爲hmi_Yanke,庫2爲hmi_Yanke2,兩個庫都存在表TDisplay網絡

則在庫1建立觸發器以下sqlserver

 1 --建立[hmi_Yanke].[dbo].[TDisplay]表的觸發器
 2 create trigger [dbo].[trigger_TDisplay]
 3 on [HMI_YanKe2].[dbo].[TDisplay]
 4 AFTER INSERT,UPDATE,DELETE
 5 as 
 6 begin
 7 declare @RowsD Int,@RowsI Int,@Dml Int
 8 --肯定是哪種dml操做
 9 Select @RowsD=Count(*) From Deleted
10 Select @RowsI=Count(*) From Inserted
11 If @RowsD=0 And @RowsI=0  
12 Goto Exit_
13 If @RowsD=0 And @RowsI>0
14     Set @Dml=1
15 Else If @RowsD>0 And @RowsI>0
16     Set @Dml=2
17 Else If @RowsD>0 And @RowsI=0
18     Set @Dml=3 
19 IF @DML=1
20 BEGIN
21 --插入
22     IF NOT EXISTS(SELECT TOP 1 1 FROM [hmi_Yanke].[dbo].[TDisplay] c,inserted i where  c.FID=i.FID)
23     begin 
24     set IDENTITY_INSERT [hmi_Yanke].[dbo].[TDisplay] ON 
25     insert into [hmi_Yanke].[dbo].[TDisplay]( [FID],FFieldCN,FFieldEN,FTable,FOrder,FType,FNullable,FLength,FWidth)
26     select [FID],FFieldCN,FFieldEN,FTable,FOrder,[FType],FNullable,FLength,FWidth from inserted
27     set IDENTITY_INSERT [hmi_Yanke].[dbo].[TDisplay] OFF
28     end
29 END
30 IF @DML=2
31 begin 
32     --修改
33     IF NOT EXISTS
34     (SELECT TOP 1 1 FROM [hmi_Yanke].[dbo].[TDisplay] c,INSERTED i 
35         where c.[FID]=i.[FID] and c.FFieldCN=i.FFieldCN and c.FFieldEN=i.FFieldEN and c.FTable=i.FTable 
36         and c.FOrder=i.FOrder and c.[FType]=i.[FType] and c.FNullable=i.FNullable and c.FLength=i.FLength 
37         and c.FWidth=i.FWidth )
38     UPDATE [hmi_Yanke].[dbo].[TDisplay] set FFieldCN=i.FFieldCN,FFieldEN=i.FFieldEN,FTable=i.FTable,FOrder=i.FOrder,[FType]=i.[FType],FNullable=i.FNullable,FLength=i.FLength,FWidth=i.FWidth
39         FROM [hmi_Yanke].[dbo].[TDisplay] c,INSERTED i where c.[FID]=i.[FID]
40     --select * from INSERTED
41     --select * from deleted
42 end
43 IF @DML=3
44     BEGIN
45     --刪除
46         IF  EXISTS(SELECT TOP 1 1 FROM  [hmi_Yanke].[dbo].[TDisplay] c,deleted d where  c.[FID]=d.[FID]) 
47         DELETE [hmi_Yanke].[dbo].[TDisplay] FROM [hmi_Yanke].[dbo].[TDisplay] c,deleted d 
48         WHERE c.[FID]=d.[FID] 
49     END
50 EXIT_: 
51 end

在庫2建立觸發器與上面相同,只須要將hmi_Yanke改成hmi_Yanke2便可測試

簡單測試後獲得正確結果spa

 1   INSERT INTO [HMI_YanKe].[dbo].[TDisplay] values('haha','haha','haha',1,'haha',1,1,1),('hehe','hehe','hehe',2,'hehe',2,2,2)
 2   INSERT INTO [HMI_YanKe2].[dbo].[TDisplay] values('xixi','xixi','xixi',1,'xixi',1,1,1),('keng','keng','keng',2,'keng',2,2,2)
 3 
 4   update [HMI_YanKe].[dbo].[TDisplay] set FFieldCN='vinjack' where FFieldCN='haha'
 5   update [HMI_YanKe2].[dbo].[TDisplay] set FFieldCN='vinjane' where FFieldCN='xixi'
 6 
 7   delete [HMI_YanKe].[dbo].[TDisplay] where FFieldCN in('vinjack','hehe')
 8   delete [HMI_YanKe2].[dbo].[TDisplay] where FFieldCN in('vinjane','keng')
 9   
10   select *  FROM [HMI_YanKe].[dbo].[TDisplay]
11   select *  FROM [HMI_YanKe2].[dbo].[TDisplay] 
相關文章
相關標籤/搜索