在存储过程中设置日期

时间:2019-10-30 12:17:56

标签: date stored-procedures

我有一个存储过程,可以在一个日期范围内查找订单。如何在存储过程中设置日期?这是我的代码。谢谢!!

我已编写查询。只需设置当前日期即可。

USE [TMWSUITE]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Proc [dbo].[DawgAlert_Completed_Order_Status] 
AS
SELECT
  orderheader.ord_number
  ,orderheader.ord_billto
  ,orderheader.ord_status
  ,orderheader.ord_origin_earliestdate
  ,orderheader.ord_origin_latestdate
  ,orderheader.ord_dest_earliestdate
  ,orderheader.ord_dest_latestdate
  ,orderheader.ord_trailer
  ,orderheader.ord_totalmiles
  ,stops.stp_city
  ,stops.stp_state
  ,stops.stp_schdtearliest
  ,stops.stp_schdtlatest
  ,stops.stp_status
  ,stops.stp_number
  ,stops.stp_sequence
  ,stops.stp_arrivaldate
  ,stops.stp_departuredate
  ,stops.cmd_code
  ,stops.cmp_name
  ,orderheader.ord_refnum
  ,orderheader.ord_description
  ,trailerprofile.trl_number
  ,trailerprofile.trl_type3
  ,city.cty_nmstct
  ,orderheader.ord_completiondate
FROM
  orderheader
  INNER JOIN stops
    ON orderheader.ord_hdrnumber = stops.ord_hdrnumber
  LEFT OUTER JOIN company
    ON orderheader.ord_company = company.cmp_id
  INNER JOIN trailerprofile
    ON orderheader.ord_trailer = trailerprofile.trl_number
  INNER JOIN city
    ON stops.stp_city = city.cty_code
WHERE
  stops.stp_departure_status = 'DNE' AND orderheader.ord_status='CMP' AND (orderheader.ord_completiondate >= @LoadDate) AND (orderheader.ord_completiondate <= @EndDate) AND (orderheader.ord_billto = @billto)

我希望存储过程运行并提取特定日期范围内的数据。因此,如果我在查询中设置当前日期。它将提取前一天或同一天的数据。无论哪种方式我都可以做到。

1 个答案:

答案 0 :(得分:0)

假设使用SQL Server,则可以使用GETDATE()函数获取当前日期。如果您希望它是日期而不是当前日期和时间,则可以使用CAST(GETDATE() AS DATE)。因此,声明变量并进行设置:

DECLARE @current_date DATETIME
SET @current_date = CAST(GETDATE() AS DATE)