1.触发器 create trigger test on a for update as update b set b.ccc = (select a.fff from inserted i where i.id = a.id )---假设id是他们关联字段 from a.inserted i2.sp create procedure test @id as int, @fff as varchar(10), as if exists (select 1 from a where a.id = @id) begin begin transaction update a set a.fff = @fff where a.id = @id if @@error <> 0 begin rollback transaction return 1 表示更新a失败 end if exists (select 1 from b where b.id = @id) begin update b set b.ccc = @fff where b.id = @id if @@error <> 0 begin rollback transaction return 2 表示更新b失败 --如果不存在就当也是成功 end end commit transaction end return 0--a表不存在可更新资料FYI
vivianfdlpw() 介绍的是“实时处理”,而这会影响你一笔事务的处理时间的,当然简单处理耗时可忽略不计。
而“延时处理”则可采用对更新标志位的轮巡处理,采用该方式的前提是,业务需求对b表更新及时率要求不高。
create trigger test
on a
for update
as
update b
set b.ccc = (select a.fff from inserted i
where i.id = a.id )---假设id是他们关联字段
from a.inserted i2.sp
create procedure test
@id as int,
@fff as varchar(10),
as
if exists (select 1 from a where a.id = @id)
begin
begin transaction
update a set a.fff = @fff where a.id = @id
if @@error <> 0
begin
rollback transaction
return 1 表示更新a失败
end
if exists (select 1 from b where b.id = @id)
begin
update b set b.ccc = @fff where b.id = @id
if @@error <> 0
begin
rollback transaction
return 2 表示更新b失败 --如果不存在就当也是成功
end
end
commit transaction
end
return 0--a表不存在可更新资料FYI
你的第二种方法,参数是哪传过来的啊?程序中?循环得到吗?
1。如果你每次只是更新一条数据,然后触发更新或者在sp同时修改b表,只问更新a表的速度,当然可以勉强说不影响2。如果是频繁更新,中间就有相同次数的触发更新或者在sp同时修改b表,速度肯定变慢所以,我觉得用触发,sp(多更新b表的东西),相对你原来的单独更新b表的整体平均速度肯定要慢!用触发器会有什么弊端呢?---弊端就是如果触发器要操作的语句太多,统计数据的方法复杂!!速度会非常慢。不过如果逻辑真是非常复杂,那慢也没有办法!!只能优化代码罗