【发布时间】:2020-07-21 20:39:39
【问题描述】:
我在 My SQL 中编写了如下存储过程:
DELIMITER //
CREATE PROCEDURE SP_Add_Expense
(
p_Expense_ID int /* = null */,
p_Project_ID int,
p_Category_ID int,
p_Sub_Category_ID int,
p_SupplierID int,
p_ExpenseDate datetime,
p_Notes text/* =null */,
p_Quantity int,
p_Price decimal(18,2),
p_CreatedBy int
)
BEGIN
IF((select count(*) from Expense where Category_ID = p_Category_ID AND Sub_Category_ID = p_Sub_Category_ID
AND SupplierID = p_SupplierID AND p_Expense_ID IS NULL) > 0)
THEN
THROW 50005, N'Expense already exists!!!', 1
ELSEIF ((select count(*) from Expense where Expense_ID = p_Expense_ID) > 0)
THEN
Update Expense set Category_ID = p_Category_ID, Sub_Category_ID = p_Sub_Category_ID, SupplierID = p_SupplierID, Quantity = p_Quantity, ExpenseDate = p_ExpenseDate,
Notes = p_Notes, Price = p_Price, ModifiedBy = p_CreatedBy, ModifiedDate = NOW(), Project_ID= p_Project_ID where Expense_ID = p_Expense_ID;
ELSE
INSERT INTO Expense (Project_ID, Category_ID, Sub_Category_ID, SupplierID, Quantity, Price, CreatedBy, CreatedDate, ExpenseDate, Notes)
VALUES (p_Project_ID, p_Category_ID, p_Sub_Category_ID, p_SupplierID, p_Quantity, p_Price, p_CreatedBy, NOW(), p_ExpenseDate, p_Notes);
END IF;
END IF;
END;
//
DELIMITER ;
它在 ELSEIF 处抛出异常。异常逻辑就像我得到的费用计数大于 0 时,我抛出异常,就像它已经存在一样。不明白是什么错误。
【问题讨论】:
标签: mysql sql stored-procedures mysql-python