如何在查询中使用 GETDATE
我正在查询数据,其中包含在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论