3

i wrote the 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

I run it:

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

and got this error:

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

why NULLIF() didn't enter Null value to _startDate???

for your convenience, task table query:

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
);

Thanks!

Gil Hadad
  • 307
  • 1
  • 4
  • 12

1 Answers1

2

You get this error because it doesn't even get to insert.

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

It fails here:

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

When you try to pass '' to DATETIME parameter.

What you trying to do:

'' -> _startDate DATETIME parameter -> INSERT -> NULLIF(_startDate, '') -> NULL

should be:

NULL -> _startDate DATETIME parameter -> INSERT -> NULL

Pass NULL directly and don't do workaround with NULLIF().

CALL task_tool.SP_create_new_task('name1 ', 'description1', NULL, NULL, 'lacation1', 1);
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275