触发器示例

/*

 在新增或者修改TableB的时候同步修改TableA的内容
 Antique
 2006-05-23

*/
SET XACT_ABORT on

begin tran

Create Table TableA
(
 [ID]  Int Identity,
 [Name]  varchar(20)
)

Create Table TableB
(
 ParentID Int,
 [Name]  varchar(20)
)

Insert Into TableA(name) Values('张三')
Select * from TableA

go
Create Trigger tUpdate
on TableB
FOR INSERT, UPDATE
as
Update TableA Set [Name] = b.[Name] from TableA a, Inserted b where a.ID = b.ParentID
go

Insert Into TableB(ParentiD, Name) Values(1, '李四')

Select * from TableA
Select * from TableB


Drop Table TableA
Drop trigger tUpdate
Drop Table TableB


Commit Tran

你可能感兴趣的:(Go)