Sql 确定作业今天是否第一次运行

Sql 确定作业今天是否第一次运行,sql,sql-server,sql-agent-job,Sql,Sql Server,Sql Agent Job,每天在第一次运行sql作业时,我都需要更新一些列。我的工作每天早上4点开始 所以我可以这样做: IF(CONVERT(VARCHAR(8),GETDATE(),108) = '04:00:00') BEGIN // Update END 但是,如果因为某种原因,我的工作不能在凌晨4点开始。如何重置这些列。任何想法、建议、帮助 我的要求如下: 我的系统从最终用户那里获取任务。此工作需要根据员工的能力将这些任务分配给他们。因此,我有一个带有EmpId、maxAssignments的表,一天中可以

每天在第一次运行sql作业时,我都需要更新一些列。我的工作每天早上4点开始

所以我可以这样做:

IF(CONVERT(VARCHAR(8),GETDATE(),108) = '04:00:00')
BEGIN
 // Update
END
但是,如果因为某种原因,我的工作不能在凌晨4点开始。如何重置这些列。任何想法、建议、帮助

我的要求如下:
我的系统从最终用户那里获取任务。此工作需要根据员工的能力将这些任务分配给他们。因此,我有一个带有EmpId、maxAssignments的表,一天中可以分配的任务的最大数量&AssignmentsCountNumber of tasks已分配列。每天在第一次运行时,我想将此AssignmentsCount值设置为0。

您应该能够在作业步骤中使用与下面所示类似的代码。SQL语句用注释分开进行解释。为简洁起见,您可以将它们组合起来,甚至将它们组合成一个单独的标量函数,该函数接受作业标识符并在第一次运行时返回true或false

DECLARE @job_id uniqueidentifier
DECLARE @run_requested_date DATETIME
DECLARE @first_run_today DATETIME

-- Use Tokens in Job Steps
-- https://msdn.microsoft.com/en-us/library/ms175575.aspx
-- Within your SQL Agent job step you can use this tokenized
-- statement to get the job_id.
--SET @job_id = CONVERT(uniqueidentifier, $(ESCAPE_NONE(JOBID)))

-- You can use this statement instead, but it will break if the
-- job name is changed and you forget to update the [name]
-- parameter below ('Test' is the job name in this example)
SELECT @job_id = job_id
  FROM msdb.dbo.sysjobs
 WHERE name = 'Test'

-- Debug statement
PRINT 'job_id = ' + CONVERT(VARCHAR(255), @job_id)

-- Get the scheduled run time for this currently
-- executing job (can alternatively use the
-- start_execution_date column instead)
SELECT @run_requested_date = run_requested_date 
  FROM msdb.dbo.sysjobactivity 
 WHERE job_id = @job_id

-- Debug statement
PRINT 'run_requested_date = ' + CONVERT(VARCHAR(50), @run_requested_date, 126)

-- For the given job, find any job history row that
-- was successfully execute at an earlier time on 
-- this date.
--
-- The msdb.dbo.agent_datetime() function is a built-in
-- function that will values from the separate date and time
-- columns in the job history table and convert them to a
-- single datetime value.
SELECT @first_run_today = msdb.dbo.agent_datetime(run_date, run_time) 
  FROM msdb.dbo.sysjobhistory 
 WHERE JOB_ID = @job_id 
   AND run_status = 1 -- Successful
   AND run_date = CONVERT(VARCHAR(10), @run_requested_date, 112) -- YYYYMMDD format compare
   AND msdb.dbo.agent_datetime(run_date, run_time) < @run_requested_date

-- Debug statement
PRINT 'first_run_today = ' + CONVERT(VARCHAR(50), @first_run_today, 126)

-- If the first_run_today variable is null then
-- that means for the current date we did not
-- any successful runs of this job before now.
IF @first_run_today IS NULL
BEGIN
  PRINT 'This *is* the first run.'
  -- First run statements go here.
END
ELSE
BEGIN
  PRINT 'This is *not* the first run.'
  -- Non first run statements go here.
END

也许在每天只运行一次的单独作业中进行更新?为什么选择凌晨4点而不是凌晨00点01分?如果时间表在凌晨3点移动,会发生什么?是否应该修改sql代码以匹配?请解释一下要求,而不是你试图提出的解决方案…@Paolo我将我的要求添加到了questionRelated:没有包含实际作业细节的表格吗?如果是这样的话,如果在分配任务时它还没有一个datetime列记录,那么它会不会没有呢?这样就使得分配变得多余,因为只要你需要,你就可以把实际的总数加起来,而且你也不需要做这个维护任务。