当表2中列的值更改时,我正在尝试更新表1中的列。我试图避免使用SQL Server触发器,因为启用触发器后,我在应用程序中遇到了很多问题。另外,它必须是表2的即时更新。非常感谢您的帮助。
谢谢
逻辑
获取受影响产品的最大到期日的状态,并根据状态更改表2中的买方
编辑。我添加了我使用的触发器。但这效率很低并且引起了问题
CREATE TRIGGER trg_Update
ON [dbo].[tblTable1]
AFTER UPDATE
AS
DECLARE
@v_orderId int,
@v_proId int,
@v_statusId int
BEGIN
SELECT @v_orderId = OrderID
FROM inserted
SELECT @v_proId = ProdID
FROM tblTable1
WHERE OrderID = @v_orderId
SELECT @v_statusId = v.VisitStatusID
FROM tblTable1 v
INNER JOIN
(
SELECT MAX(DueDate) AS MaxDueDate, ProdID
FROM tblTable1
GROUP BY ProdID
) t2 ON v.ProdID = t2.ProdID
AND v.DueDate = t2.MaxDueDate
WHERE v.ProdID = @v_proId
IF @v_statusId IN (5, 25)
BEGIN
UPDATE tblTable2 SET Buyer= 'CP1' WHERE ProdID = @v_proId
END
ELSE
BEGIN
UPDATE tblTable2 SET Buyer= 'CP2' WHERE ProdID = @v_proId
END
END