通过表数据执行时函数不返回结果

时间:2016-06-15 18:00:08

标签: sql-server tsql

我在我的网站上使用了一个自行开发的批处理引擎。我试图创建一个能让我获得" 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' (几个小时)

当我执行此函数显式发送这些示例参数时,它会返回我想要的内容。像这样: 选择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个已启用的作业,因此它不会通过很多记录进行分块...

编辑:哦,我投入了子选择,因为我认为可能在尝试调用WEREN&T已启用的记录功能时遇到问题,所以我只是想确保它只是处理9个工作。

1 个答案:

答案 0 :(得分:0)

我弄明白发生了什么。

当传递的@hoursToRun值只有一个"小时"时,该函数永远不会返回任何内容。

例如:

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

要解决此问题,我只需要添加一个逗号到最后,它就可以正常工作,如下所示:

set @hoursToRun = @hoursToRun + ','