MySQL 存储过程 - 将整数变量添加到日期 returns null

MySQL Stored Procedures - Adding integer variable to date returns null

我正在编写我的第一个 mysql 程序。我有一个 date 变量和 2 个整数变量。我将两个整数变量相乘并将所得整数添加到 date 变量。

例如:

pint=6;
noftimes=3
sdate=2020-05-05
totDays=pint*noftimes
edate=2020-05-05+totDays

我可以添加多个 pint*noftimes 但不能添加 sdate+totDays。而如果我添加 sdate+10 那么我会得到一个正确的增量日期值。以下是我的程序:

DELIMITER $$
CREATE DEFINER=`pattu`@`localhost` PROCEDURE `getFieldWorkDates`(IN `p_id` INT, OUT `totDays` INT, OUT `edate` DATE)
BEGIN
DECLARE noftimes int;
DECLARE pint int;
DECLARE sdate DATE;
DECLARE tdays int;
SELECT startDate into sdate from projects where idprojects=p_id;
SELECT projectDuration into noftimes from projects where idprojects=p_id;
SELECT recFreq into pint from projects where idprojects=p_id;
SET totDays=pint*noftimes;
SET edate = sdate+(pint*noftimes);

END$$
DELIMITER ;

当我执行此操作时,我收到消息,您的查询已成功执行。 0 行受影响

SET edate = sdate+(pint*noftimes);

您不能将整数添加到日期。使用 DATE_ADD() 函数。


CREATE 
DEFINER=pattu@localhost
PROCEDURE getFieldWorkDates ( IN p_id INT, 
                              OUT totDays INT, 
                              OUT edate DATE)
SELECT recFreq * projectDuration, 
       startDate + INTERVAL recFreq * projectDuration DAY 
INTO totDays, 
     edate 
FROM projects 
WHERE idprojects=p_id;