2016-06-15 56 views
0

我有一個本地的批處理引擎,我在我的網站上使用。我正在嘗試創建一個函數,讓我獲得所有啓用的作業的「NextRunDate」。當通過表數據執行時,函數不返回結果

這裏的功能:

create function dbo.uf_BatchJob_Get_NextRunDate 
(
    @daysToRun varchar(19), 
    @hoursToRun varchar(84) 
) 
returns datetime as begin 
    set @daysToRun = replace(@daysToRun, ' ', '') 
    set @hoursToRun = replace(@hoursToRun, ' ', '') 

    declare @now datetime = getdate() 
    declare @currentYear int = datepart(yyyy, @now) 
    declare @currentMonth int = datepart(MM, @now) 
    declare @currentDay int = datepart(dd, @now) 
    declare @currentHour int = datepart(hh, @now) 
    declare @timeCheck datetime = convert(varchar(4), @currentYear) + '/' + convert(varchar(2), @currentMonth) + '/' + convert(varchar(2), @currentDay) + ' ' + convert(varchar(2), @currentHour) + ':00:00.000' 

    declare @foundDay bit = 0 
    declare @foundHour bit = 0 

    while (@foundDay = 0 or @foundHour = 0) begin 
     -- increment by 1 hour 
     set @timeCheck = dateadd(hh, 1, @timeCheck) 

     declare @dayOfWeekCheck int = datepart(dw, @timeCheck) - 1 -- 0 based versus 1 based 
     declare @hourCheck int = datepart(hh, @timeCheck) 

     if (charindex(cast(@dayOfWeekCheck as varchar(1)), @daysToRun) > 0) begin 
      set @foundDay = 1 
     end else begin 
      set @foundDay = 0 
     end 

     declare @hourIndex int = charindex(cast(@hourCheck as varchar(2)), @hoursToRun) 
     declare @characterBefore varchar(1) = substring(@hoursToRun, @hourIndex - 1, 1) 
     declare @characterAfter varchar(1) = substring(@hoursToRun, @hourIndex + len(@hourCheck), 1) 

     if (@characterBefore = '') begin 
      set @characterBefore = ',' 
     end 

     if (@characterAfter = '') begin 
      set @characterAfter = '' 
     end 

     if (@foundDay = 1) begin 
      if (@hourIndex > 0 and @characterBefore = ',' and @characterAfter = ',') begin 
       set @foundHour = 1 
      end else begin 
       set @foundHour = 0 
      end 
     end 
    end 

    -- it's been found, let's return the value 
    return @timeCheck 
end 

對於信息,所述@daysToRun@hoursToRun參數被構造是這樣的: '0,1,3,6'(天)或「2,圖4,圖9,15 ,19,23'(幾小時)

當我執行這個函數顯式地發送這些示例參數時,它正好返回我想要的。像這樣: select dbo.uf_BatchJob_Get_NextRunDate('0,1,3,6','2,4,9,15,19,23')

但是,如果我將這個函數調用到實時數據中,它從不迴應。下面是我如何做到這一點:

select x.BatchProcessId, 
     x.BatchName, 
     x.DaysToRun, 
     x.HoursToRun, 
     dbo.uf_BatchJob_Get_NextRunDate(x.DaysToRun, x.HoursToRun) [NextRunDate] 
from (
    select bp.BatchProcessId, 
      bp.BatchName, 
      bps.DaysToRun, 
      bps.HoursToRun 
    from dbo.BatchProcess bp 
    join dbo.BatchProcessSchedule bps on bps.BatchProcessId = bp.BatchProcessId 
     and bps.[Enabled] = 1 
    where bp.[Enabled] = 1 
) x 

爲什麼它不會響應以內聯方式運行它?子查詢只返回9個已啓用的作業,所以它不會通過非常多的記錄來分塊...

編輯:哦,我拋出子選擇,因爲我認爲它可能一直有問題試圖調用函數用於尚未啓用的記錄,所以我只想確保它只處理9個作業。

+0

你得到你的沒有函數調用的預期結果? –

+0

@MahediSabuj「沒有函數調用」你的意思是直接調用它,就像這個'select dbo.uf_BatchJob_Get_NextRunDate('0,2','1,3')'。如果是這樣,是的 – ganders

+0

不,試着刪除這部分形式的選擇查詢,'dbo.uf_BatchJob_Get_NextRunDate(x.DaysToRun,x.HoursToRun)[NextRunDate]'並檢查你是否得到預期的結果? –

回答

0

我想清楚發生了什麼事。

@hoursToRun值傳遞只有一個「小時」,這是該函數從不返回任何東西。

例如:

@hoursToRun = '13' -- which would be 1:00pm

爲了解決這個問題,現在我只需要一個逗號添加到末尾,它工作正常,像這樣:

set @hoursToRun = @hoursToRun + ','