SQL server游标运行两次

SQL server游标运行两次,sql,sql-server,Sql,Sql Server,假设我有一个光标,可以找到要更新的数据 declare @index int; declare cursor1 cursor for select table1_index from table where (table1_date < dateadd(dd, -365, getdate())) open cursor1 fetch next from cursor1 into @index while @@fetch_status = 0 begin update table1

假设我有一个光标,可以找到要更新的数据

declare @index int;
declare cursor1 cursor for
select table1_index from table where (table1_date < dateadd(dd, -365, getdate()))

open cursor1 
fetch next from cursor1 into @index
while @@fetch_status = 0
begin
  update table1
  set table1_field = SOMETHING
  where table1_index = @index

  if @ERROR = 0
  insert into audit_trail
  values(getdate(), table1_index)

  fetch next from cursor1 into @index
end
close cursor1 
deallocate cursor1 
上述代码位于存储过程中,并将由调度器每天运行,例如每天12:00 am

我的问题是,如果调度程序运行存储的pro。例如,在2017年6月17日凌晨12:00,但仍在运行,例如在2017年6月17日下午5:00

如果我在2017年6月17日下午3:00运行完全相同的代码, 光标是否会从更新的表中选择数据?或来自未由调度程序更新的表的数据

非常感谢

declare cursor1 cursor for
select table1_index from table where (table1_date < dateadd(dd, -365, getdate())) 
and table1_index NOT IN (SELECT table1_index FROM audit_trail)

您需要添加条件和table1\u索引,而不是从审核跟踪中选择table1\u索引。我希望它能为您工作。

取决于您的事务模式,但假设ACID符合性模式和过程外的事务边界:它将使用第一个过程开始运行之前的数据。由于您有隐式事务,整个update语句可能会阻止表的进一步更新,2017年6月17日下午3:00,将等待update语句完成。由于这是一个巨大的UPDATE语句,它会变成表锁或页锁,并且会阻塞其他事务,直到它完成。我的UPDATE语句看起来不大,执行起来也不会花费太多时间。无论如何,感谢您的回答:。我可以了解一些有关ACID合规模式的信息吗?谢谢