如何在查询中使用 GETDATE

发布于 2025-01-09 09:42:10 字数 1157 浏览 0 评论 0原文

我正在查询数据,其中包含在 EXEC 命令末尾的查询末尾插入的日期,但如果可能的话,我想使用 GETDATE 函数来查询昨天,因为我计划按计划而不是手动运行这些查询。我不想每天都去编辑日期。

USE [EXP]
GO

DECLARE @DateFrom datetime;
SET @DateFrom = DATEADD(day, -1, cast(getdate() as date));

/****** Object:  Table [dbo].[Trans]    Script Date: 6/11/2021 9:25:00 AM ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Trans]') AND type in (N'U'))
DROP TABLE [dbo].[Trans]
GO

/****** Object:  Table [dbo].[Trans]    Script Date: 6/11/2021 9:25:00 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Trans](
    [Account] [varchar](8) NULL,
    [Amount] [money] NOT NULL,
    [typename] [varchar](20) NULL,
    [DateEffective] [datetime] NULL,
    [TranDesc] [varchar](200) NULL,
    [DRAmount] [money] NULL,
    [CRAmount] [money] NULL,
    [CUUser] [varchar](50) NULL,
    [Deleted] [bit] NULL
) ON [PRIMARY]
GO


--set identity_insert Trans on
insert into Trans
    ([Account]
    ,[Amount]
    ,[typename]
    ,[DateEffective]
    ,[TranDesc]
    ,[DRAmount]
    ,[CRAmount]
    ,[CUUser]
    ,[Deleted])
EXEC [FER_DP].[DP].[dbo].[Excel_Trans] @DateFrom;

I am querying data with a date I insert at the end of the query at the end of EXEC command but I want to use the GETDATE function to query yesterday if possible since I plan on running these queries on a schedule and not manually. I don't want to have to go and edit the date every day.

USE [EXP]
GO

DECLARE @DateFrom datetime;
SET @DateFrom = DATEADD(day, -1, cast(getdate() as date));

/****** Object:  Table [dbo].[Trans]    Script Date: 6/11/2021 9:25:00 AM ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Trans]') AND type in (N'U'))
DROP TABLE [dbo].[Trans]
GO

/****** Object:  Table [dbo].[Trans]    Script Date: 6/11/2021 9:25:00 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Trans](
    [Account] [varchar](8) NULL,
    [Amount] [money] NOT NULL,
    [typename] [varchar](20) NULL,
    [DateEffective] [datetime] NULL,
    [TranDesc] [varchar](200) NULL,
    [DRAmount] [money] NULL,
    [CRAmount] [money] NULL,
    [CUUser] [varchar](50) NULL,
    [Deleted] [bit] NULL
) ON [PRIMARY]
GO


--set identity_insert Trans on
insert into Trans
    ([Account]
    ,[Amount]
    ,[typename]
    ,[DateEffective]
    ,[TranDesc]
    ,[DRAmount]
    ,[CRAmount]
    ,[CUUser]
    ,[Deleted])
EXEC [FER_DP].[DP].[dbo].[Excel_Trans] @DateFrom;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文