SQL 过程中的 IF 子句
IF clause in SQL procedure
我用未来 15 年的所有日期创建了日期 table...
CREATE TABLE [dbo].[All_Dates](
[Year] [int] NOT NULL,
[Quarter] [nvarchar](6) NOT NULL,
[Month] [nvarchar](6) NOT NULL,
[Week] [nvarchar](7) NOT NULL,
[Day] [nvarchar](50) NOT NULL,
[DayKey] [tinyint] NOT NULL,
[DateName] [nvarchar](50) NOT NULL,
[DateKey] [date] NOT NULL,
PRIMARY KEY CLUSTERED
(
[DateKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
这样做的目的是在某些报告程序中使用该时间段。
所以,现在问题就在这里,当我需要在程序开始时有一些 IF 子句时,当需要参数时,因为我需要让客户选择月度或季度报告。对于每月,我有代码:
ALTER procedure [dbo].[proc1]
@dt date
as
declare @startdate date
,@enddate date
SET @StartDate = (select min(datekey) from TDW.dbo.All_Dates where month = convert(varchar(6),@dt,112))
SET @EndDate = (select max(datekey) from TDW.dbo.All_Dates where month = convert(varchar(6),@dt,112))
所以,我需要类似 if time_range=quarter then...
假设 '201701'
等是您的 month
列的有效值,那么您应该处理开始和结束月份,然后找到它们的日期。这是一个示例:
DECLARE @dt DATE = '2017-01-01'
DECLARE @time_range VARCHAR(10) = 'quarter'
DECLARE @startmonth VARCHAR(6) = convert(VARCHAR(6), @dt, 112) -- e.g. '201701'
IF @time_range = 'quarter' SET @dt = DATEADD(m, 2, @dt) -- Assumed it is OK to change @dt
DECLARE @endmonth VARCHAR(6) = convert(VARCHAR(6), @dt, 112) -- e.g. '201703'
DECLARE @startdate DATE, @enddate DATE
SET @StartDate = (select min(datekey) from TDW.dbo.All_Dates where month = @startmonth)
SET @EndDate = (select max(datekey) from TDW.dbo.All_Dates where month = @endmonth)
如果需要,您可以使用多个 IF
,甚至 CASE WHEN ... THEN ... ELSE ... END
语句。
我用未来 15 年的所有日期创建了日期 table...
CREATE TABLE [dbo].[All_Dates](
[Year] [int] NOT NULL,
[Quarter] [nvarchar](6) NOT NULL,
[Month] [nvarchar](6) NOT NULL,
[Week] [nvarchar](7) NOT NULL,
[Day] [nvarchar](50) NOT NULL,
[DayKey] [tinyint] NOT NULL,
[DateName] [nvarchar](50) NOT NULL,
[DateKey] [date] NOT NULL,
PRIMARY KEY CLUSTERED
(
[DateKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
这样做的目的是在某些报告程序中使用该时间段。 所以,现在问题就在这里,当我需要在程序开始时有一些 IF 子句时,当需要参数时,因为我需要让客户选择月度或季度报告。对于每月,我有代码:
ALTER procedure [dbo].[proc1]
@dt date
as
declare @startdate date
,@enddate date
SET @StartDate = (select min(datekey) from TDW.dbo.All_Dates where month = convert(varchar(6),@dt,112))
SET @EndDate = (select max(datekey) from TDW.dbo.All_Dates where month = convert(varchar(6),@dt,112))
所以,我需要类似 if time_range=quarter then...
假设 '201701'
等是您的 month
列的有效值,那么您应该处理开始和结束月份,然后找到它们的日期。这是一个示例:
DECLARE @dt DATE = '2017-01-01'
DECLARE @time_range VARCHAR(10) = 'quarter'
DECLARE @startmonth VARCHAR(6) = convert(VARCHAR(6), @dt, 112) -- e.g. '201701'
IF @time_range = 'quarter' SET @dt = DATEADD(m, 2, @dt) -- Assumed it is OK to change @dt
DECLARE @endmonth VARCHAR(6) = convert(VARCHAR(6), @dt, 112) -- e.g. '201703'
DECLARE @startdate DATE, @enddate DATE
SET @StartDate = (select min(datekey) from TDW.dbo.All_Dates where month = @startmonth)
SET @EndDate = (select max(datekey) from TDW.dbo.All_Dates where month = @endmonth)
如果需要,您可以使用多个 IF
,甚至 CASE WHEN ... THEN ... ELSE ... END
语句。