最明顯的解決方案是在DOM表上編寫更新觸發器。這個觸發器將處理TOM和COM表。
Select 1 ID, 10 Value
into DOM
union all
Select 2, 20
union all
Select 3, 30
Select 1 ID, 10 Value
into TOM
union all
Select 2, 20
union all
Select 3, 30
Select 1 ID, 10 Value
into COM
union all
Select 2, 20
union all
Select 3, 30
drop trigger DomUpdate
create trigger DomUpdate on DOM
after Update
as
begin
Update TOM
set value = i.value
from TOM t
INNER JOIN
inserted I ON I.ID = T. ID
Update COM
set value = i.value
from TOM t
INNER JOIN
inserted I ON I.ID = T. ID
end
GO
Select D.value
FROM
DOM D
INNER JOIN
COM C ON C.ID = D.ID
INNER JOIN
TOM T ON T.ID = D. ID
WHERE
D.VALUE <> 50
begin tran t1
Update D
set D.value = 50
FROM
DOM D
INNER JOIN
COM C ON C.ID = D.ID
INNER JOIN
TOM T ON T.ID = D. ID
WHERE
D.VALUE <> 50
--rollback tran t1
commit tran t1
Select * from DOM
Select * from COM
Select * from TOM
你不能用一條'update'語句更新多個表。 –
您無法使用單個更新語句更新多個表,但可以將所有更新放入單個事務中,以便所有更新都將被提交或回滾。 –