在vb.net表适配器中使用的派生表中转换失败

时间:2015-05-13 17:08:53

标签: sql-server vb.net parameters casting derived-table

我在尝试将参数插入到派生表中时遇到问题,该派生表是vb.net应用程序中表适配器填充查询的一部分。

当我尝试通过数据集设计器的预览窗口或使用我的datatableadapter.fill方法在vb.net中使用查询时,我收到“从字符串转换日期和/或时间时转换失败”应用。

SELECT Date, SUM(RegHours) AS RegTotal, SUM(OTHours) AS OTTotal, SUM(VacHours) AS VacTotal, SUM(SickHours) AS SickTotal

FROM (SELECT EmployeeId, Date, RegHours, OTHours, (CASE WHEN TaskId=4 THEN RegHours ELSE NULL END) AS VacHours, (CASE WHEN TaskId=5 THEN RegHours ELSE NULL END) AS SickHours FROM TimeEntry

WHERE (EmployeeId=@EmployeeId) AND (Date>=@StartDate)) AS SubTable

GROUP BY Date

当我只有@EmployeeId参数时查询工作正常,因此问题在于日期转换。

如果只执行派生表部分,查询也可以正常工作:

SELECT EmployeeId, Date, RegHours, OTHours, (CASE WHEN TaskId=4 THEN RegHours ELSE NULL END) AS VacHours, (CASE WHEN TaskId=5 THEN RegHours ELSE NULL END) AS SickHours 

FROM TimeEntry 

WHERE (EmployeeId=@EmployeeId) AND (Date>=@StartDate)

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我能够通过在表定义中添加VacHours和StatHours作为计算列,然后在不需要派生表的情况下进行选择来解决此问题。

SELECT Date, SUM(RegHours) AS TotalReg, SUM(OTHours) AS TotalOT, SUM(VacHours) AS TotalVac, SUM(StatHours) AS TotalStat
FROM TimeEntry
WHERE (EmployeeId = @EmployeeId) AND (Date >= @StartDate) AND (Date <= @EndDate)
GROUP BY Date

我怀疑我可能仍然没有填写正确的数据类型或正确投射,但这确实为我提供了一个简单的解决方案。