我编写了一个SQL触发器,首先验证项目是否具有折扣价格,如果不是则必须引发错误。触发器的第二部分需要检查文档的日期并确保该项目未过期。我的触发器会同时执行一个但不能同时执行两个操作。任何人都可以提供协助,我的代码如下。
SELECT @DocType = DocType, @OrderDate = OrderDate, @InvDate = InvDate
FROM InvNum
WHERE AutoIndex = @Invoice
SELECT @DocState = DocState
FROM InvNum
WHERE AutoIndex = @Invoice
SELECT @Account = AccountID
FROM InvNum
WHERE AutoIndex = @Invoice
SELECT @Valid = iStockCodeID
FROM _btblInvoiceLines
LEFT JOIN InvNum ON iInvoiceID = AutoIndex
LEFT JOIN _etblVDAR ON iARAPID = AccountID
LEFT JOIN _etblVDLnAR ON iVDID = IDVD
WHERE iStockID = @StockCode AND iARAPID = @Account
SELECT @ValidPO = iStockCodeID
FROM _btblInvoiceLines
LEFT JOIN InvNum ON iInvoiceID = AutoIndex
LEFT JOIN _etblVDAP ON iARAPID = AccountID
LEFT JOIN _etblVDLnAP ON iVDID = IDVD
WHERE iStockID = @StockCode AND iARAPID = @Account
select @dExpDate=dexpdate
from _btblInvoiceLines
inner join _etblVDLnAR on _btblInvoiceLines.iStockCodeID = _etblVDLnAR.iStockID
where _btblInvoiceLines.iStockCodeID = iStockCodeID
-- BEGIN
-- IF (@DocType = 4 OR @DocType = 1) AND @DocState <> 7
-- BEGIN
-- IF @Module = 0 AND @Valid IS NULL
-- BEGIN
-- RAISERROR ('Message from Management:
--You are not allowed to select an item for which no Volume Discount Price has been entered for this customer.
--The transaction will be rolled back. ', 16, 1)
-- ROLLBACK TRANSACTION
-- End
IF (@DocType = 4 AND @DocState IN (0,1,2,3)) AND @OrderDate>=@dExpDate
BEGIN
RAISERROR ('Message from Management:
Please check expiry date! . ', 16, 1)
ROLLBACK TRANSACTION
END
END
END
END