Reputation: 701
Overall I am attempting to look through an auditing table to see if a specific date range has already been processed. In this specific case, @fstartrangedate
will be NULL, as the 2016-11-01
start date has not been found. This should make it to the main else condition. @superceded
should be set to 0, @type
(sumtype) should be set to WEEKLY, @days
should be set to 7.
I make it to the correct main else condition. Even make it to Weekly -- and yet, somehow @type is set to SPECIAL.
Can someone please take a look @this. I've probably done something really silly, but I just don't see it.
Here's the code:
-- 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
Here are the debug output messages when executed:
(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)*
Here are the variable output messages when executed:
Upvotes: 1
Views: 41
Reputation: 38023
Let me introduce you to my buddy 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;
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'
Ironically, you would not have had (found?) that error if you were not debugging your code with those print statements.
rextester demo to show the error, and how begin/end
would fix it: http://rextester.com/CSUM92647
Upvotes: 3