-1

我有一个使用以下 sql 获取一些数据的应用程序:

SELECT Sum(fieldname)          AS fieldNameHere,
   Sum(Cast(error AS INT))  AS Error,
   Sum(jsperror)            AS JSPError,
   Sum(linkerror)       AS LinkError
FROM   schemaname.sessiondata session
WHERE  starttime >= :startTime0
   AND starttime < :endTime0
   AND fieldname = :fieldNameVal0
UNION ALL
SELECT Sum(fieldname)          AS fieldNameHere,
   Sum(Cast(error AS INT))  AS Error,
   Sum(jsperror)            AS JSPError,
   Sum(linkerror)       AS LinkError
FROM   schemaname.sessiondata session
WHERE  starttime >= :startTime1
   AND starttime < :endTime1
   AND fieldname = :fieldNameVal1
UNION ALL
SELECT Sum(fieldname)          AS fieldNameHere,
   Sum(Cast(error AS INT))  AS Error,
   Sum(jsperror)            AS JSPError,
   Sum(linkerror)       AS LinkError
FROM   schemaname.sessiondata session
WHERE  starttime >= :startTime2
   AND starttime < :endTime2
   AND fieldname = :fieldNameVal2

调试和检查代码参数值是:

startTime0=Wed Sep 12 22:00:00 BST 2012
endTime0=Wed Sep 12 23:00:00 BST 2012
hitPrices0=1 
startTime1=Wed Sep 12 23:00:00 BST 2012
endTime1=Thu Sep 13 00:00:00 BST 2012
hitPrices1=1
startTime2=Thu Sep 13 00:00:00 BST 2012
endTime2=Thu Sep 13 01:00:00 BST 2012 
hitPrices2=1

然后在 SQL-server management studio 中运行以下 SQL 提供正确的数据:

SELECT Sum(fieldname)          AS fieldNameHere,
   Sum(Cast(error AS INT))  AS Error,
   Sum(jsperror)            AS JSPError,
   Sum(linkerror)       AS LinkError
FROM   schemaname.sessiondata session
WHERE  starttime >= '2012-09-12 22:00:00'
   AND starttime < '2012-09-12 23:00:00'
   AND fieldname = 1
UNION ALL
SELECT Sum(fieldname)          AS fieldNameHere,
   Sum(Cast(error AS INT))  AS Error,
   Sum(jsperror)            AS JSPError,
   Sum(linkerror)       AS LinkError
FROM   schemaname.sessiondata session
WHERE  starttime >= '2012-09-12 23:00:00'
   AND starttime < '2012-09-13 00:00:00'
   AND fieldname = 1
UNION ALL
SELECT Sum(fieldname)          AS fieldNameHere,
   Sum(Cast(error AS INT))  AS Error,
   Sum(jsperror)            AS JSPError,
   Sum(linkerror)       AS LinkError
FROM   schemaname.sessiondata session
WHERE  starttime >= '2012-09-13 00:00:00'
   AND starttime < '2012-09-13 01:00:00'
   AND fieldname = 1

注意:这不是 startDate 和 endDate 的错误,参数与字段不同。startDate 和 end date 参数表示该特定时间间隔的 startdatetime 和 enddatetime。startDate 字段表示会话开始时间。

应用程序中返回的错误结果:

[null, null, null, null]
[2, 1, 0, 0]
[null, null, null, null]

以及来自 sqlsms 的正确结果:

NULL    NULL    NULL    NULL
NULL    NULL    NULL    NULL
NULL    NULL    NULL    NULL

我无法弄清楚这是为什么?

4

1 回答 1

0

我发现格式化日期对象并作为字符串传递以在 SQL 上设置参数解决了这个问题。

于 2012-09-13T15:32:09.803 回答