这是我的存储过程。我有问题为Declared变量赋值。当我执行它时,插入和更新命令工作正常,但声明变量的VALUE保持为0;但我在数据库中有一些价值。我怎么能做这个Corectly。
BEGIN
DECLARE PaidFee INT DEFAULT 0;
DECLARE DueFee INT DEFAULT 0;
DECLARE CourseFee INT DEFAULT 0;
INSERT INTO `creditdirectory`(`TypeID`, `PersonName`, `CreditBy`, `PersonID`, `ModeOfPayment`,`Details`,`Amount`,`CompanyID`)
VALUES(1,PersonName,CreditBy, AddmissionID, ModeOfPayment, 'Installment', PaidAmount,
CompanyID);
SELECT `CourseFee`,`PaidFee`,`DueFee` INTO CourseFee,PaidFee,DueFee FROM `studentcoursedetails` WHERE `ID`= CourseID;
SET PaidFee = PaidFee + PaidAmount;
SET DueFee = CourseFee - PaidFee;
IF (NextDueDate !='') THEN
UPDATE `studentcoursedetails` SET `PaidFee` = PaidFee, `DueFee` = DueFee, `DueDate` = NextDueDate WHERE `ID`= CourseID;
ELSE
UPDATE `studentcoursedetails` SET `PaidFee` = PaidFee, `DueFee` = DueFee, `DueDate` = NULL WHERE `ID` = CourseID;
END IF;
END
答案 0 :(得分:1)
不要使用具有相同列名的变量,查询将优先考虑表列名称。
一个好主意是使用带前缀的变量:
BEGIN
DECLARE p_PaidFee INT DEFAULT 0;
DECLARE p_DueFee INT DEFAULT 0;
DECLARE p_CourseFee INT DEFAULT 0;
INSERT INTO `creditdirectory`(`TypeID`, `PersonName`, `CreditBy`, `PersonID`, `ModeOfPayment`,`Details`,`Amount`,`CompanyID`)
VALUES(1,PersonName,CreditBy, AddmissionID, ModeOfPayment, 'Installment', PaidAmount,
CompanyID);
SELECT `CourseFee`,`PaidFee`,`DueFee` INTO p_CourseFee,p_PaidFee,p_DueFee FROM `studentcoursedetails` WHERE `ID`= CourseID;
SET p_PaidFee = p_PaidFee + PaidAmount;
SET p_DueFee = p_CourseFee - p_PaidFee;
IF (NextDueDate !='') THEN
UPDATE `studentcoursedetails` SET `PaidFee` = p_PaidFee, `DueFee` = p_DueFee, `DueDate` = NextDueDate WHERE `ID`= CourseID;
ELSE
UPDATE `studentcoursedetails` SET `PaidFee` = p_PaidFee, `DueFee` = p_DueFee, `DueDate` = NULL WHERE `ID` = CourseID;
END IF;
END