我是创建触发器的新手。当我更新我的一个表时,请帮助创建更新触发器,它也会更新其他两个表。我有三个名为tblDHColl,tblDHSurv和tblDHLithology的表。所有表格都有一些共同的字段,例如“数据集”,“#3; HoleID”和“#39;和'验证'。我想要的是,当我更新'验证'在具有值" 1"的tblDHColl表中的字段,它应该更新'验证'其他两个表中具有相同值的字段。我有两个值1(True)和0(False)来更新此列。请帮我创建一个触发器来更新其他两个表。
答案 0 :(得分:-1)
我在tblDHColl上创建了一个触发器。因此,如果您将tblDHColl.Validated从1更新为0或0更新为1,则触发器将更新其他2个表。
create table tblDHColl ( HoleID int , Dataset int , Validated int)
create table tblDHSurv ( HoleID int , Dataset int , Validated int)
create table tblDHLithology ( HoleID int , Dataset int , Validated int)
insert into tblDHColl
select 1,100, 1
union all select 2,500, 0
union all select 3,700, 1
union all select 4,300, 0
union all select 5,900, 1
insert into tblDHSurv
select 1,100, 1
union all select 2,500, 0
union all select 3,700, 1
union all select 4,300, 0
union all select 5,900, 1
insert into tblDHLithology
select 1,100, 1
union all select 2,500, 0
union all select 3,700, 1
union all select 4,300, 0
union all select 5,900, 1
go
create trigger tr1 on tblDHColl
for update
as
begin
declare @Validated_new int , @Validated_old int , @HoleID int
select @HoleID = i.HoleID ,@Validated_new=i.Validated from inserted i
select @Validated_old=d.Validated from deleted d
if (@Validated_new <>@Validated_old )
begin
update tblDHSurv
set Validated = @Validated_new
where HoleID = @HoleID
update tblDHLithology
set Validated = @Validated_new
where HoleID = @HoleID
end
end
go
update tblDHColl
set Validated = 1
where HoleID = 2
select * from tblDHColl where HoleID = 2
select * from tblDHSurv where HoleID = 2
select * from tblDHLithology where HoleID = 2