单调乏味的SQL Server TVP:parameter.value.getTime不是datetime的函数

我正在尝试使用TVP,并且在使用DateTime参数时不断收到此错误。

构build请求时的exception是:

  days = Math.floor((parameter.value.getTime() - UTC_EPOCH_DATE.getTime()) / (1000 * 60 * 60 * 24)); ^ 

例外:

TypeError:parameter.value.getTime不是一个函数

代码看起来像这样

 /*declare table*/`` let table = { columns: [ { name: 'a', type: TYPES.VarChar, length: 50, nullable: true }, { name: 'b', type: TYPES.Int}, { name: 'c', type: TYPES.DateTime} ], rows: [ ['hello tvp', 777,'05/08/07 12:35 PM'], ['OLO', 45,'05/08/16 1:30 AM'] ] }; /*request code*/ var request = new Request("MyCustomStoredProcedure", function (err, rowCount) { if (!err) { callback(err) logger.info("rowCount", rowCount) } else { callback(rowCount) logger.error("Error => ", err) } }); request.addParameter('tvp', TYPES.TVP, table); request.on('row', function (columns) { logger.info("data", columns) }); connection.callProcedure(request); CREATE TYPE TestType AS TABLE (a VARCHAR(50), b INT, c DateTime); CREATE PROCEDURE MyCustomStoredProcedure (@tvp TestType readonly) AS SELECT * FROM @tvp 

看着data-types.js的繁琐代码,我发现parameter.value是一个string,而不是一个对象。

不知道我在这里做错了什么。

我试过了

  • 没有datetime – 工作
  • DateTime2 – 传入的表格数据stream(TDS)远程过程调用(RPC)协议stream不正确。 参数2(“”):数据types0x03未知。
  • 与https://github.com/patriksimek/node-mssql ,但内部它再次使用Tedious

我用Varchar而不是DateTime,它解决了我的问题。 我知道这不是最好的解决办法,但为了时间的利益,我拿了它。

你的date格式有问题。 JavaScript的date时间有一个时区元素SQL的date时间没有时区。

尝试

 ['hello tvp', 777,new Date('05/08/07 12:35 PM')] 

我遇到了同样的问题,在时区中添加parsing它。