无法将 mysql 存储过程中日期时间的“”更改为 null

failed to change '' to null for datetime in mysql stored procedure

我写了存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `SP_create_new_task`(
IN  _taskName       VARCHAR(30),
IN  _description    VARCHAR(500),
IN  _startDate      DATETIME,
IN  _endDate        DATETIME,
IN  _lacation       VARCHAR(30),
IN  _subTo          INT
)
BEGIN

INSERT INTO tm_tasks (taskName, description, startDate, endDate, lacation,      subTo)
VALUES (_taskName, _description, NULLIF(_startDate,''), NULLIF(_endDate,''), _lacation, _subTo);

END

我运行它:

call task_tool.SP_create_new_task('name1 ', 'description1', '', '', 'lacation1', 1);

并收到此错误:

Error Code: 1292. Incorrect datetime value: '' for column '_startDate' at row 1

为什么 NULLIF() 没有向 _startDate 输入 Null 值???

为方便起见,任务table查询:

CREATE TABLE tm_tasks(
ID              INT         PRIMARY KEY AUTO_INCREMENT,
taskName        VARCHAR(30) NOT NULL,
description     VARCHAR(500),
joinDate        TIMESTAMP,
startDate       DATETIME    NULL DEFAULT NULL,
endDate         DATETIME    NULL DEFAULT NULL,
lacation        VARCHAR(30),
subTo           INT DEFAULT NULL,
ignoreRow       TINYINT(1)  DEFAULT 0
);

谢谢!

您收到此错误是因为它甚至无法插入。

Error Code: 1292. Incorrect datetime value: '' for column '_startDate' at row 1

这里失败了:

CREATE DEFINER=`root`@`localhost` PROCEDURE `SP_create_new_task`(
IN  _taskName       VARCHAR(30),
IN  _description    VARCHAR(500),
**IN  _startDate      DATETIME**,

当您尝试将 '' 传递给 DATETIME 参数时。

你想做什么:

'' -> _startDate DATETIME 参数 -> INSERT -> NULLIF(_startDate, '') -> NULL

应该是:

NULL -> _startDate DATETIME 参数 -> INSERT -> NULL

直接传递 NULL 并且不对 NULLIF() 进行变通。

CALL task_tool.SP_create_new_task('name1 ', 'description1', NULL, NULL, 'lacation1', 1);