Header
表:
HeadeID Status
----------------
1 Open
Detail
表:
DetailID HeaderID Status
-------------------------
1 1 Close
2 1 Close
答案 0 :(得分:0)
SQL Server允许使用触发器。简单来说,触发器是在更新表时执行的存储过程。您通常使用触发器来强制执行业务规则/逻辑。
因此,要应用于您的情况,请在Details表上放置一个Trigger,它将检查当前HeaderID的所有行是否已设置为Closed,然后更新Header表。
CREATE TRIGGER Detail_Update_Header
ON Detail
AFTER Insert, Update
AS
BEGIN
SET NOCOUNT ON;
declare @currentHeaderID int;
SELECT @currentHeaderID = i.HeadeID from inserted i;
IF((SELECT COUNT(*) FROM Detail WHERE HeaderID = @currentHeaderID) > (SELECT COUNT(*) FROM Detail WHERE HeaderID = @currentHeaderID AND Status = 'Closed'))
BEGIN
UPDATE Header
SET Status = 'closed';
END
END
GO
答案 1 :(得分:-1)
如果我理解正确,你需要这样的东西:
UPDATE dbo.Header
set Status = b.Status
from dbo.Header H
inner join dbo.Detail b ON
b.HeaderID = H.HeaderID