日期时间比较更新两列
当新作业添加到数据库时,我有一个由 SQL Server 创建的日期时间
。将作业添加到数据库后 3 小时且尚未完成,应更改具有唯一作业 ID 的作业的日志级别和状态。
这是我到目前为止所拥有的。 (不检查状态)
use DistributedComputing
if exists (select * from sys.objects where type = 'P' and name = 'proc_dc_timeout')
drop procedure proc_dc_timeout
go
create procedure proc_dc_timeout
AS
declare @JobID int
begin
if exists (select StartDate from Job where JobID = @JobID and DATEDIFF(hh,StartDate,GETDATE()) > 3 )
update Job
set LogLevel = 1, Status = 'error'
end
go
execute proc_dc_timeout
但是如果我运行查询,则不会影响任何行。请帮忙
I have a datetime
created by SQL Server when a new job is added to the database. 3 hours after the job is added to the database and it is not completed the loglevel and the status of the job with a unique job id should be changed.
This is what I have so far. (Without checking the status)
use DistributedComputing
if exists (select * from sys.objects where type = 'P' and name = 'proc_dc_timeout')
drop procedure proc_dc_timeout
go
create procedure proc_dc_timeout
AS
declare @JobID int
begin
if exists (select StartDate from Job where JobID = @JobID and DATEDIFF(hh,StartDate,GETDATE()) > 3 )
update Job
set LogLevel = 1, Status = 'error'
end
go
execute proc_dc_timeout
But if I run the query no rows are effected. Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的变量
@JobID
已声明,但未设置 - 因此 - 您的IF
语句永远不会成功,因为与 NULL 值的任何比较都将返回 false。您需要将值分配给变量,或者使用IS NULL
比较Your variable
@JobID
is declared, but not set - so - yourIF
statement never succeeds, because any comparison with NULL value will return false. You either need to assign the value to variable, or useIS NULL
comparison正如上面的答案所指出的,JobID 为空。假设您想要检查和更新所有作业,这可能就是您想要的:
我使用 DATEDIFF(分钟...) 而不是小时,但这只是个人偏好,以确保关闭-一个错误不会造成如此大的差异。
As the answers above note, the JobID is null. Assuming that you want to check and update all jobs, this might be what you want:
I used DATEDIFF(minute...) instead of hour, but that's just a personal preference to make sure an off-by-one error doesn't make such a huge difference.
有几件事......
您的
@JobID
变量似乎应该是过程的参数。你永远不会给这个变量赋值。其次,您的
DATEDIFF(hh, StartDate, GETDATE()) > 3
最有可能更改为DATEDIFF(hh, StartDate, GETDATE()) >= 3
;正如目前所写,在达到 4 小时标记之前,您不会进行任何更新。此外,您是否考虑过使用 ALTER PROCEDURE 语法,而不是在每次运行时检查过程是否存在并删除/重新创建?
A couple of things ...
It seems like your
@JobID
variable should be a parameter of the procedure. You are never giving a value to this variable.Second, your
DATEDIFF(hh, StartDate, GETDATE()) > 3
should most likely be changed toDATEDIFF(hh, StartDate, GETDATE()) >= 3
; as currently written you wouldn't do any updates until the 4-hour mark is reached.Additionally, have you considered using the
ALTER PROCEDURE
syntax rather than checking for the existence of the procedure and dropping/recreating at each run?这将不起作用,因为您没有为
@JobID
设置任何值,它将为null
并且您存在的查询将始终失败this will not work as you not setting any value for
@JobID
and it will benull
and you exists query will fail always执行过程时您没有给 @JobID 赋值,并且参数声明位于错误的位置。
You didn't give @JobID a value when executing the procedure and the parameter declaration is in the wrong location.