mysql存储过程中的日期值错误

时间:2015-06-05 02:45:02

标签: mysql sql node.js stored-procedures

我的客户端请求有效负载javascript / angular

active: 1
appId: "asdf"
description: "asdf"
from: "06/16/2015"
name: "gdsfg"
to: "06/18/2015"

Node.js代码

var query = "SET @start = '" + request.body.from  + "'; \
                         SET @end = '" + request.body.to + "'; \
                         SET @event_id = " + rows.insertId + "; \
                         CALL day(@start, @end, @event_id);";

错误返回

{ [Error: ER_TRUNCATED_WRONG_VALUE: Incorrect date value: '06/16/2015' for column 'start' at row 2]
  code: 'ER_TRUNCATED_WRONG_VALUE',
  errno: 1292,
  sqlState: '22007',
  index: 3 }

存储过程:

(实质上它需要from和to日期,并根据差异创建行数。)

CREATE DEFINER=`root`@`localhost` PROCEDURE `day`(start DATE, end DATE, event_id INT)
BEGIN
    WHILE start <= end DO
        INSERT INTO day(date, event_id) VALUES(start, event_id);
        SET start = start + 1;
    END WHILE;
END

问题:

不确定导致错误的原因,任何人都可以帮忙

编辑 - 查询输出

SET @start = '06/16/2015'; SET @end = '06/18/2015'; SET @event_id = 3; CALL day(@start, @end, @event_id);

2 个答案:

答案 0 :(得分:0)

我猜测date被存储为日期。但是start不是,可能只是一个看起来的整数,就像一个日期。

如果是这样,这将解决您的问题:

WHILE start <= end DO
    INSERT INTO day(date, event_id) VALUES(start, event_id);
    SET start = date_add(@tart, interval 1 day);
END WHILE;

编辑:

这不是问题。问题出在调用代码中。所以试试:

var query = "SET @start = str_to_date('" + request.body.from  + "', '%m/%d/%Y'); \
                         SET @end = str_to_date('" + request.body.to + "', '%m/%d/%Y'); \
                         SET @event_id = " + rows.insertId + "; \
                         CALL day(@start, @end, @event_id);";

答案 1 :(得分:0)

您应在查询中使用'yyyy-mm-dd'格式,而不是'mm / dd / yyyy'。因为您的程序参数是日期类型

您的查询应该是这样

SET @start = '2015-06-16'; SET @end = '2015-06-18'; SET @event_id = 3; CALL day(@start, @end, @event_id);