我正在尝试在编辑表格时更新表格的状态列。我在另一个表上使用类似的触发器来更新此状态但是当我尝试将触发器放在表本身上时它将无法运行。
ALTER TRIGGER dbo.tr_Invoice_SetInvoiceStatus_Update
ON dbo.Invoice
AFTER INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @InvoiceID int
DECLARE @TotalInvoice DECIMAL(18,2)
DECLARE @TotalPayments DECIMAL(18,2)
DECLARE @InvoiceStatus NVARCHAR(50)
DECLARE @InvoiceStatusID INT
SELECT @InvoiceID = ID
FROM Inserted i
SELECT @TotalPayments = IsNull(Sum(ph.Amount), 0)
FROM PaymentHistory ph
WHERE InvoiceID = @InvoiceID
SELECT @TotalInvoice = ISNULL(Total, 0)
FROM Invoice
WHERE ID = @InvoiceID
IF (@TotalPayments > 0)
BEGIN
IF (@TotalPayments >= @TotalInvoice)
BEGIN
SELECT @InvoiceStatus = 'Paid'
END
ELSE
BEGIN
SELECT @InvoiceStatus = 'Partially Paid'
END
END
ELSE
BEGIN
SELECT @InvoiceStatus = 'Open'
END
SELECT @InvoiceStatusID = ID
FROM dbo.InvoiceStatus
WHERE [Name] = @InvoiceStatus
UPDATE dbo.Invoice
SET InvoiceStatusID = @InvoiceStatusID
WHERE ID = @InvoiceID
END
GO
任何帮助都会很棒?