【发布时间】:2018-12-11 13:07:51
【问题描述】:
我有一个简单的查询,我经常使用它来获取特定时间段内某条生产线上的所有重量数据。
现在我想将我必须的查询转换为 Visual Studio 报告,以便其他人可以运行报告而不是询问我。
作为第一步,我尝试在 Management Studio 中运行查询以查看它是否有效。
DECLARE @ProductionLineID as int
Set @ProductionLineID = 11
DECLARE @Start as timestamp
Set @Start = '2018-06-29 19:20'
DECLARE @End as timestamp
Set @End = '2018-06-30 19:10'
SELECT [ProductionLineId]
,[Timestamp]
,[ActiveRecipe]
,[ActualWeight]
,[SetWeight]
,[SetBoxWeight]
,[SetMaxTolerance]
,[SetMinTolerance]
,[DeviationFromSetWeight]
,[AmountOfProductInBox]
,[AverageProductWeightPerBox]
,[ActualSealTemp]
,[ActualCuttingTemp]
,[ParametersChanged]
,[rejectError]
FROM [PP_Staging].[NIV].[Packaging]
where ProductionLineId = @ProductionLineID
and timestamp between @Start and @End
order by Timestamp
这会导致以下错误
Msg 257, Level 16, State 3, Line 5
Implicit conversion from data type varchar to timestamp is not allowed. Use the CONVERT function to run this query.
Msg 257, Level 16, State 3, Line 7
Implicit conversion from data type varchar to timestamp is not allowed. Use the CONVERT function to run this query.
所以我尝试了相关部分的转换功能:
DECLARE @Start as timestamp
Set @Start = convert(timestamp, '2018-06-29 19:20')
DECLARE @End as timestamp
Set @End = convert(timestamp, '2018-06-30 19:10')
这会导致
Msg 8115, Level 16, State 2, Line 9
Arithmetic overflow error converting expression to data type datetime.
所以第一个问题是如何输入/设置此参数以便测试报告?
我知道如果我手动输入我的代码可以正常工作,所以我基本上是在寻找一种将手动查询转换为带有变量的报告的方法。一旦我有了它,我就可以尝试弄清楚如何设置报告。
工作代码示例:
SELECT [ProductionLineId]
,[Timestamp]
,[ActiveRecipe]
,[ActualWeight]
,[SetWeight]
,[SetBoxWeight]
,[SetMaxTolerance]
,[SetMinTolerance]
,[DeviationFromSetWeight]
,[AmountOfProductInBox]
,[AverageProductWeightPerBox]
,[ActualSealTemp]
,[ActualCuttingTemp]
,[ParametersChanged]
,[rejectError]
FROM [PP_Staging].[NIV].[Packaging]
where ProductionLineId = 11
and timestamp between '2018-06-29 19:20' and '2018-06-30 19:10'
order by Timestamp
【问题讨论】:
-
我无法复制错误,但是,我想知道它是否使用了不确定的日期格式。请尝试使用确定性格式,例如
yyyy-MM-ddThh:mm:ss。例如SET @Start = CONVERT(timestamp, '2018-06-29T19:20:00');。这行得通吗? -
你应该使用数据类型'datetime'而不是'timestamp':stackoverflow.com/questions/7105093/…
-
"时间戳语法已弃用。此功能将在 Microsoft SQL Server 的未来版本中删除。避免在新的开发工作中使用此功能,并计划修改当前使用此功能的应用程序。"时间戳不是用来保存日期/时间值的,它不能与 ANSI 时间戳相比较。 Sql server 时间戳用于行版本控制。见here。就像 j03p 说的,你需要使用 Datetime 或 datetime2。
-
@Larnu 我尝试将它与不同的变体一起使用,例如是否包含毫秒,没有用
-
@j03p 感谢您提供的信息,它在使用日期时间作为类型时有效。
标签: sql sql-server datetime