日期时间比较更新两列

发布于 2025-01-05 09:38:48 字数 576 浏览 1 评论 0原文

当新作业添加到数据库时,我有一个由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

审判长 2025-01-12 09:38:48

您的变量 @JobID 已声明,但未设置 - 因此 - 您的 IF 语句永远不会成功,因为与 NULL 值的任何比较都将返回 false。您需要将值分配给变量,或者使用 IS NULL 比较

Your variable @JobID is declared, but not set - so - your IF statement never succeeds, because any comparison with NULL value will return false. You either need to assign the value to variable, or use IS NULL comparison

梨涡少年 2025-01-12 09:38:48

正如上面的答案所指出的,JobID 为空。假设您想要检查和更新所有作业,这可能就是您想要的:

create procedure proc_dc_timeout
AS
update Job
set LogLevel = 1, Status = 'error'
where DATEDIFF( minute, StartDate, GETDATE() ) > 180
    and Status not in ( 'error' )

我使用 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:

create procedure proc_dc_timeout
AS
update Job
set LogLevel = 1, Status = 'error'
where DATEDIFF( minute, StartDate, GETDATE() ) > 180
    and Status not in ( 'error' )

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.

一城柳絮吹成雪 2025-01-12 09:38:48

有几件事......

您的 @JobID 变量似乎应该是过程的参数。你永远不会给这个变量赋值。

其次,您的 DATEDIFF(hh, StartDate, GETDATE()) > 3 最有可能更改为 DATEDIFF(hh, StartDate, GETDATE()) >= 3;正如目前所写,在达到 4 小时标记之前,您不会进行任何更新。

此外,您是否考虑过使用 ALTER PROCEDURE 语法,而不是在每次运行时检查过程是否存在并删除/重新创建?

ALTER PROCEDURE proc_dc_timeout ( @JobID INT )
AS
    BEGIN
        UPDATE j
        SET j.[LogLevel = 1,
            j.[Status] = 'error'
        FROM [Job] j
        WHERE j.[JobID] = @JobID
            AND DATEDIFF(hh, j.[StartDate], GETDATE()) >= 3
    END
GO

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 to DATEDIFF(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?

ALTER PROCEDURE proc_dc_timeout ( @JobID INT )
AS
    BEGIN
        UPDATE j
        SET j.[LogLevel = 1,
            j.[Status] = 'error'
        FROM [Job] j
        WHERE j.[JobID] = @JobID
            AND DATEDIFF(hh, j.[StartDate], GETDATE()) >= 3
    END
GO
七分※倦醒 2025-01-12 09:38:48

这将不起作用,因为您没有为 @JobID 设置任何值,它将为 null 并且您存在的查询将始终失败

this will not work as you not setting any value for @JobID and it will be null and you exists query will fail always

心是晴朗的。 2025-01-12 09:38:48

执行过程时您没有给 @JobID 赋值,并且参数声明位于错误的位置。

CREATE PROCEDURE proc_dc_timeout
   @JobID int // <== declare parameter here
AS
BEGIN
    //...
END
go

EXECUTE proc_dc_timeout @JobID // <== put value here instead @JobID

You didn't give @JobID a value when executing the procedure and the parameter declaration is in the wrong location.

CREATE PROCEDURE proc_dc_timeout
   @JobID int // <== declare parameter here
AS
BEGIN
    //...
END
go

EXECUTE proc_dc_timeout @JobID // <== put value here instead @JobID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文