在我的数据库中,我有这个触发器,它可以工作,但它看起来非常糟糕的代码。
当用户注册您的活动时,表格HOURS
包含idWork
历史记录。每个用户都分配了一个个人idWork
CREATE TRIGGER [dbo].[trg_HOURS_002]
ON [dbo].[HOURS]
AFTER INSERT,UPDATE
AS
BEGIN
Update [HOURS]
set idWork = (select U.idWork
from [USER] U
where U.id = (select inserted.idUser from inserted))
where id = (select inserted.id from inserted)
END
GO
你能帮助我做得更好吗?
答案 0 :(得分:1)
尝试将sub-query
更改为JOIN
。这样的事情。
UPDATE A
SET idWork = b.idWork
FROM [HOURS] A
JOIN (SELECT U.idWork,
i.id
FROM [USER] U
JOIN inserted I
ON U.id = i.idUser) B
ON a.id = b.id
或
UPDATE A
SET idWork = b.idWork
FROM [HOURS] A
JOIN inserted I
ON a.id = I.id
JOIN [USER] U
ON U.id = i.idUser