我无法理解为什么这会给我错误"Cannot construct data type date, some of the arguments have values which are not valid."
这是我的疑问:
SELECT TOP 1 pc.Comment AS [Comment], pc.[Month] AS [Month], pc.[Year] AS [Year]
FROM PECCS.dbo.ProjectComment pc
WHERE pc.ProjectId = 11501 AND
pc.Type = 'OYEO' AND
(DATEFROMPARTS(pc.[Year], pc.[Month], 1) <= DATEFROMPARTS(2017, 12, 1)) AND
pc.ResourceCategoryID = 1
ORDER BY pc.[Year] DESC, pc.[Month] DESC
我传递的参数有效。我认为问题是表中有记录,其中Month = 0.我相信这会导致问题:(DATEFROMPARTS(pc.[Year], pc.[Month], 1)
答案 0 :(得分:0)
数据库中的[Month]是INT列,还是带有月份名称的VARCHAR?使用类似的列来测试它,它给了我相同的错误,直到我将[Month]更改为不同的列[Year of Year],即INT。然后它工作正常。
答案 1 :(得分:0)
我通过将0的月份转换为默认月值来解决问题。在这种情况下,我选择了12:
SELECT TOP 1 pc.Comment AS [Comment], pc.[Month] AS [Month], pc.[Year] AS [Year]
FROM PECCS.dbo.ProjectComment pc
WHERE pc.ProjectId = 11501 AND
pc.Type = 'OYEO' AND
(DATEFROMPARTS(pc.Year, CASE WHEN pc.[Month] = 0 THEN 12 ELSE pc.[Month] END, 1) <= DATEFROMPARTS(@Year, @Month, 1)) AND
pc.ResourceCategoryID = 1
ORDER BY pc.[Year] DESC, pc.[Month] DESC