应用程序向sql-server management studio返回不同的结果

时间:2012-09-13 14:12:18

标签: sql sql-server hibernate java-ee

我有一个使用以下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

我无法理解为什么会这样?

1 个答案:

答案 0 :(得分:0)

我发现格式化日期对象并作为字符串传递来设置SQL上的参数修复了这个问题。