一级:表A:id content 表B:id content
1 a1 1 b1
2 a2 2 b2
二级:id SourceTable SourceId
1 0 1
2 1 1
三级:id SourceTable SourceId
1 0 1
2 1 1
二级表分别是表A和表B的数据,SourceTable为0代表的是表A的数据,为1代表的是表B的数据,三级表中的SourceTable分别对应二级表中的数据,0代表的是表A对应的二级表中的SourceTable值为0的那些数据的相关数据,1代表的是表B对应的二级表中的SourceTable值为1的那些数据的相关数据。现在我要建立一个触发器。就是当我删除表A的某条数据时,就删除二级的和三级的。现在二级的是可以删除了,但是三级的就不知道怎么建立触发器了,请高手赐教!
1 a1 1 b1
2 a2 2 b2
二级:id SourceTable SourceId
1 0 1
2 1 1
三级:id SourceTable SourceId
1 0 1
2 1 1
二级表分别是表A和表B的数据,SourceTable为0代表的是表A的数据,为1代表的是表B的数据,三级表中的SourceTable分别对应二级表中的数据,0代表的是表A对应的二级表中的SourceTable值为0的那些数据的相关数据,1代表的是表B对应的二级表中的SourceTable值为1的那些数据的相关数据。现在我要建立一个触发器。就是当我删除表A的某条数据时,就删除二级的和三级的。现在二级的是可以删除了,但是三级的就不知道怎么建立触发器了,请高手赐教!
直接在触发器里面先删除三级的,再删除二级的。大致如下:create trigger my_trig on a
as
begin
delete 三级表 from 三级表 t where SourceTable = 0 and id = (select id from 二级表 where SourceTable = 0 and id = (select id from deleted))
delete 二级表 from 二级表 t where SourceTable = 0 and id = (select id from deleted)
endcreate trigger my_trig on b
as
begin
delete 三级表 from 三级表 t where SourceTable = 1 and id = (select id from 二级表 where SourceTable = 1 and id = (select id from deleted))
delete 二级表 from 二级表 t where SourceTable = 1 and id = (select id from deleted)
end