我有一个表格来保存任务列表,这些任务需要在特定的日期和时间处理。棘手的部分是这些任务是递归的,运行时间必须基于 5 个不同的参数来计算。
通过 UDF 计算运行时间很简单:
Function dbo.task_next_run(
@task_type varchar(10),
@task_schedule_day_of_week varchar(20),
@task_schedule_time varchar(20),
@task_period smallint,
@last_run datetime
)
Returns datetime
...
...
...
Return @next_run
我最后的任务查询是这样的:
SELECT id,
task_name,
last_run
From tasks
Where dbo.task_next_run
(
task_type, @task_schedule_day_of_week,
@task_schedule_time, @task_period, @last_run
) < getdate() and
dbo.task_next_run
(
task_type, @task_schedule_day_of_week,
@task_schedule_time, @task_period, @last_run
) > last_run
我的问题是在 where 子句中运行相同的函数 2 次。我需要一个解决方案来在 where 子句中使用计算值作为别名。