----------求一个简单的触发器,(没写过触发器,应该是很简单的)----------
TA
id mtype mk
1 aa 123
2 bb 123
TB
id ntype Statusts
1 aa 0123
2 bb 1123
3 bb 010
----------------
求一触发器,当更新TA中的mk时,TB中对应的ts也更新。
触发TB的条件是ntype=mtype and Status=0。
---
如对TA操作
update TA set mk= '999 ' where mtype= 'bb '
那么TB经触发器的影响后应该为:
TB
id ntype Statusts
1 aa 0123
2 bb 1123
3 bb 0999
------------求此触发器
[解决办法]
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