日期:2014-05-18  浏览次数:20424 次

sql2000触发器
T1

  Code state




T2
id code(外建,引用的是T1的code)  



要求 删除T2 中 code = 1 的记录 ,同时要更新 T1 中的state值!
 

------解决方案--------------------
SQL code
Create trigger tr_T2 on T2
for update
as
update t1
set State=1
from deleted d 
inner join t1 on d.Code=t1.Code

------解决方案--------------------
SQL code
create trigger my_trig on t1 for delete
as
begin
  update t1 set state = ... from t1 , deleted d where t1.code = d.code 
end

------解决方案--------------------
create trigger my_trig on t1 for delete
as
begin
update t1 set state = ... from t1 , deleted d where t1.code = d.code 
end




------解决方案--------------------
探讨
如果state 为1 则 current 清空为0 否则 current 自增

max值是 当current重置为0时 则判断 current > max 为真则 max = current

count 则表示统计的是state = 1次数

------解决方案--------------------
SQL code

create trigger tri_T2_deleted
for delete
as
begin
update T set state=需要更新的值
from T1 t,Deleted d where t.code=d.code
end