SQL Server 2012:我在使用If-Else if-Else块时遗漏了什么?

时间:2017-03-28 20:36:01

标签: sql-server tsql

总体而言,我正在尝试查看审核表,以查看是否已处理特定日期范围。在此特定情况下,@fstartrangedate将为NULL,因为尚未找到2016-11-01开始日期。这应该是主要的其他条件。 @superceded应设置为0,@type(sumtype)应设置为WEEKLY,@days应设置为7.

我将其设为正确的主要条件。甚至将它设置为Weekly - 然而,某种程度上@type被设置为SPECIAL。

有人可以看看@this。我可能做了一些非常愚蠢的事情,但我只是没有看到它。

以下是代码:

-- determine if this is a daily, weekly, monthly or special run
declare @superceded bit
declare @type varchar(50)
declare @foundSameStartDate bit
declare @days int
declare @vdays int
declare @fLoadId int

-- incoming parameters
declare @startrangedate date
declare @endrangedate date

-- found startdate
declare @fstartrangedate date

--debug
select @fstartrangedate as fstartrangedate

-- validation test --
declare @vstartrangedate date
declare @vendrangedate date

set @startrangedate = cast('2016-11-01' as date);
-- debug
select @startrangedate as startrangedate

set @endrangedate = cast('2016-11-08' as date);

select 
    @fstartrangedate = StartRangeDateTime,
    @fLoadId = LoadId
from 
    FleetMatching.Audit_LoadTracker
where 
    StartRangeDateTime IN (select StartRangeDateTime
                           from FleetMatching.Audit_LoadTracker
                           where IsSuperceded = 0
                             and IsLoadCompleted = 1
                             and StartRangeDateTime = @startrangedate)

 -- debug
 select @fstartrangedate as fstartrangedate

 IF(@fstartrangedate is not null)
 BEGIN
     print 'found start date does = startdate'
     -- valid start/end dates for a full month

     select @vstartrangedate = dateadd(month, datediff(month, 0, @startrangedate), 0) 
     select @vendrangedate = dateadd(day, -1, dateadd(month, datediff(month, -1, @startrangedate), 0))

     select @days =  datediff(day, @startrangedate, @endrangedate)
     select @vdays = datediff(day, @vstartrangedate, @vendrangedate)

     -- we found an existing range which will be superseded by this
     -- execution/load
     set @superceded = 1

  -- debug
  select @days as [Days]
  select @type as SumType

  if(@days = 1)
    set @type = 'DAILY'
  else if(@days = 7)
    set @type = 'WEEKLY'
  else if((@days >= 27) AND (@days <= 31))
    set @type = 'MONTHLY'
  else
    print 'found condition-special'
    set @type = 'SPECIAL'
  END
ELSE
 Begin
  print 'Reached MAIN-IF Else Condition'
  print 'found start date does NOT .eq. startdate'
  set @superceded = 0
  select @days = datediff(day,@startrangedate,@endrangedate)
  -- debug
  select @days as [Days]
  select @type as SumType

  if(@days = 0)
    begin
     print 'daily as 0'
     set @type = 'DAILY'
    end
  else if(@days = 7)
    begin
      print 'weekly as 7';
      set @type = 'WEEKLY';
    end
  else if(@days >= 28 AND @days <= 31)
   begin
     print 'monthly as 28->31'
     set @type = 'MONTHLY'
   end
  else
    print 'else condition=special'
    set @type = 'SPECIAL'
 END

 -- DEBUG
 select @superceded as IsSuperceded
 select @type as SumType

以下是执行时的调试输出消息:

 (1 row(s) affected)

 (1 row(s) affected)

 (1 row(s) affected)

 Reached MAIN-IF Else Condition

 found start date does NOT .eq. startdate

 (1 row(s) affected)

 (1 row(s) affected)

 weekly as 7

 (1 row(s) affected)

 (1 row(s) affected)*

以下是执行时的变量输出消息:

ssms_output

1 个答案:

答案 0 :(得分:3)

让我向您介绍我的好友<DataGrid AutoGenerateColumns="False" x:Name="datagrid" Background="{x:Null}"> <DataGrid.Columns> <DataGridTextColumn Header="AgName" Binding="{Binding AgName}" /> <DataGridTextColumn Header="AuxReasonDescription" Binding="{Binding AuxReasonDescription}" /> </DataGrid.Columns> </DataGrid>

case

<小时/> 错误在于:

set @type = case 
     when @days = 0 then 'Daily'
     when @days = 7 then 'Weekly'
     when @days >=28 and @days <= 31 then 'Monthly'
     else 'Special'
     end;

具有讽刺意味的是,如果您没有使用这些打印语句调试代码,那么您就不会遇到(发现?)错误。

rextester演示以显示错误,if (@days = 0) begin print 'daily as 0' set @type = 'DAILY' end else if (@days = 7) begin print 'weekly as 7'; set @type = 'WEEKLY'; end else if (@days >= 28 and @days <= 31) begin print 'monthly as 28->31' set @type = 'MONTHLY' end else print 'else condition=special' /* < ----- without begin and end, else stops after the print and the following `set` is outside of the flow control */ set @type = 'SPECIAL' 将如何修复错误:http://rextester.com/CSUM92647