Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何使用时间戳作为参数_Sql_Sql Server_Datetime - Fatal编程技术网

Sql 如何使用时间戳作为参数

Sql 如何使用时间戳作为参数,sql,sql-server,datetime,Sql,Sql Server,Datetime,我经常使用一个简单的查询来获取特定时间段内某条生产线上的所有权重数据 现在,我想将我的查询转换为VisualStudio报告,以便其他人可以运行该报告,而不是询问我 作为第一步,我尝试在ManagementStudio中运行一个查询,看看它是否有效 DECLARE @ProductionLineID as int Set @ProductionLineID = 11 DECLARE @Start as timestamp Set @Start = '2018-06-29 19:20' DECLA

我经常使用一个简单的查询来获取特定时间段内某条生产线上的所有权重数据

现在,我想将我的查询转换为VisualStudio报告,以便其他人可以运行该报告,而不是询问我

作为第一步,我尝试在ManagementStudio中运行一个查询,看看它是否有效

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

检查[PP_Staging].[NIV].[Packaging]的定义。我想您会发现timestamp的数据类型实际上是DATETIME或DATETIME2。然后更改变量声明,如下所示以匹配

DECLARE @ProductionLineID as int
Set @ProductionLineID = 11
DECLARE @Start as DATETIME
Set @Start = '2018-06-29 19:20'
DECLARE @End as DATETIME
Set @End = '2018-06-30 19:10' 

我不能复制这个错误,但是,我想知道是不是使用了不确定的日期格式。请尝试确定性格式,例如
yyyy-MM-ddThh:MM:ss
。例如
SET@Start=CONVERT(时间戳'2018-06-29T19:20:00')。这是否有效?您应该使用数据类型“datetime”而不是“timestamp”:“时间戳语法已被弃用。此功能将在Microsoft SQL Server的未来版本中删除。请避免在新的开发工作中使用此功能,并计划修改当前使用此功能的应用程序。”时间戳不用于保存日期/时间值,它与ANSI时间戳不可比较。Sql server时间戳用于行版本控制。看见正如j03p所说,您需要使用Datetime或datetime2。@Larnu我曾尝试将其用于不同的变体,如是否包含毫秒,但没有work@j03p感谢您提供的信息,当使用datetime作为类型时,它会起作用。