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

----------求一个简单的触发器,(没写过触发器,应该是很简单的)----------

TA
id   mtype     mk          
1       aa         123
2       bb         123

TB
id   ntype     Status ts
1       aa         0 123
2       bb         1 123
3       bb         0 10


----------------
求一触发器,当更新TA中的mk时,TB中对应的ts也更新。

触发TB的条件是ntype=mtype   and   Status=0。

---
如对TA操作

update   TA   set   mk= '999 '   where   mtype= 'bb '

那么TB经触发器的影响后应该为:
TB
id   ntype     Status ts
1       aa         0 123
2       bb         1 123
3       bb         0 999

------------求此触发器

------解决方案--------------------
create trigger t_update on ta
for update
as
begin
update tb
set tb.ts = ta.mk
from inserted ta
where tb.ntype=ta.mtype and tb.Status = 0
end
go
------解决方案--------------------
create trigger auto_update on TB
as
update TB set ts = mk from inserted a where id = a.id and Status = 0
------解决方案--------------------
CREATE TRIGGER sync_update ON TA
FOR UPDATE
AS
if exists (select 1 from TB where ntype=INSERTED.mtype and Status=0)
update TB set ts=INSERTED.mk

------解决方案--------------------
create trigger trigger_update on dbo.TA
for update
as
declare @type varchar(10)
declare @ts int
select @type=i.mytype,@ts=i.mk from inserted i
update TB set ts=@ts where ntype=@type and status=0
------解决方案--------------------
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'AA ' AND type = 'TR ')
DROP TRIGGER AA

CREATE TRIGGER AA
ON TA
FOR UPDATE
AS
UPDATE TB SET TB.TS=TA.MK
FROM INSERTED A WHERE ID=A.ID AND STATUS = 0