以下是删除发票的存储过程。发票包含一个或多个发票行,发票行包含一篇或多篇文章。
以下是它的工作原理:
以下表格受到影响:
下面是SP,但由于一些奇怪的原因我在运行时遇到错误(我正在尝试删除的发票当然存在)。有人能看出它有什么问题吗?
提前致谢!
ALTER PROCEDURE usp_DeleteInvoice
@InvoiceID int
AS
BEGIN
BEGIN TRY
BEGIN TRANSACTION;
WITH fr AS (
SELECT ArticleID, SUM(Quantity) AS ReclaimedQuantity
FROM Invoiceline
WHERE InvoiceID = @InvoiceID
GROUP BY ArticleID
)
UPDATE Article
SET Quantity = Quantity + fr.ReclaimedQuantity
FROM Article INNER JOIN
fr ON Article.ArticleID = fr.ArticleID;
DELETE FROM Invoiceline
WHERE InvoiceID = @InvoiceID;
DELETE FROM Invoice
WHERE InvoiceID = @InvoiceID; <- Error here
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
RAISERROR ('Couldn't delete invoice!',16,1)
END CATCH
END
GO
EXEC usp_DeleteInvoice 8
编辑: 我收到以下错误消息(第36行= WHERE InvoiceID = @InvoiceID;):
这是发票表的结构(来自其中一张发票的数字):
答案 0 :(得分:1)
查找错误编写的触发器,以便一次只处理一条记录。