SQL Server 代理 - 获取我自己的 job_id

发布于 2024-10-03 07:07:04 字数 1183 浏览 2 评论 0原文

我正在运行安装了 Service Pack 1 的 SQL Server 2008 64 位开发人员版。 我有一份 SQL Server 代理工作。在这个工作中,我想获取我自己工作的 job_id。
在 MSDN 上 (http://msdn.microsoft.com /en-us/library/ms175575(v=SQL.100).aspx) 您可以找到在作业步骤中使用令牌的说明。哇,太棒了,这就是我要找的!!只需使用(JOBID)即可。
从 SQL Server 2005 SP1 开始,您必须使用像 $(ESCAPE_NONE(JOBID)) 这样的宏。没问题。
但如果你尝试这个例子:

DECLARE @name NVARCHAR(128)
select @name =  name from msdb.dbo.sysjobs where job_id = $(ESCAPE_SQUOTE(JOBID))
PRINT @name

你会得到:
“ESCAPE_SQUOTE”附近的语法不正确。 (Microsoft SQL Server,错误:102)
好的,现在从头开始:

PRINT N'$(ESCAPE_SQUOTE(JOBID))'  

结果为 0xE33FE637C10B3C49A6E958BB3EF06959 但 job_id 是 37E63FE3-0BC1-493C-A6E9-58BB3EF06959
我认为“N”隐式转换为 (JOBID) 的 NVARCHAR...
好吧,我想我必须关心(JOBID)的数据类型。在《SQL Server 2008 Administration》一书中的第 168/169 页中,还有一个使用 (JOBID) 的示例:

declare @jobid binary(16)
SELECT @jobid =Convert(Uniqueidentifier,$(ESCAPE_NONE(JOBID)))

结果为:
“(”附近的语法不正确。(Microsoft SQL Server,错误:102)
我现在完全糊涂了。请有人帮助我提供好的建议或解决方案。感谢各种帮助。

此致 赫尔穆特

I'm running a SQL Server 2008 64 bit Developer Edition with Service Pack 1 installed.
I have a SQL Server Agent Job. Within this job I want to get the job_id of my own job.
On MSDN (http://msdn.microsoft.com/en-us/library/ms175575(v=SQL.100).aspx) you can find a description of using tokens in job steps. Wow, great, that's what I'm looking for!! Just use (JOBID).
Since SQL Server 2005 SP1 you have to use macro like $(ESCAPE_NONE(JOBID)). No problem.
But if you try the example:

DECLARE @name NVARCHAR(128)
select @name =  name from msdb.dbo.sysjobs where job_id = $(ESCAPE_SQUOTE(JOBID))
PRINT @name

you get:
Incorrect syntax near 'ESCAPE_SQUOTE'. (Microsoft SQL Server, Error: 102)
Ok, now from the scratch:

PRINT N'$(ESCAPE_SQUOTE(JOBID))'  

results in 0xE33FE637C10B3C49A6E958BB3EF06959 but the job_id is
37E63FE3-0BC1-493C-A6E9-58BB3EF06959
The "N'" I think makes an implicit conversion to NVARCHAR of the (JOBID)...
Ok, I think I have to care about the datatype of (JOBID). In the book "SQL Server 2008 Administration" on page 168/169 there's also an example of using (JOBID):

declare @jobid binary(16)
SELECT @jobid =Convert(Uniqueidentifier,$(ESCAPE_NONE(JOBID)))

results in:
Incorrect syntax near '('. (Microsoft SQL Server, Error: 102)
I'm totally confused now. Could please someone help me with a good advice or solution. Every kind of help is appreciated.

Best regards
Helmut

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

对你而言 2024-10-10 07:07:04

我们最近在这方面遇到了麻烦,并且没有遵循您在 MSDN 中找到的路线。相反,我们直接按名称从 dbo.sysjobs 恢复 jobid(与您的示例相反),然后在作业中使用它来检查执行状态(如果作业状态发生更改,则退出长时间运行的 while 循环)。

declare @jobid uniqueidentifier
SELECT @jobid = job_id from msdb.dbo.sysjobs where name = '[blah]'

We had trouble with this recently and did not go the route you found in MSDN. Instead, we recovered the jobid from dbo.sysjobs by name directly (the opposite of your example) and then used that within the job to check execution status (exiting out of long running while loop if job state had changed).

declare @jobid uniqueidentifier
SELECT @jobid = job_id from msdb.dbo.sysjobs where name = '[blah]'
旧城空念 2024-10-10 07:07:04

感谢您的回答。问题是我试图解析作业步骤中的语句。然后我得到了这个错误。运行作业时没有问题。
我现在最好的解决方案是:

declare @JobID uniqueidentifier
SELECT @JobID = $(ESCAPE_NONE(JOBID));
PRINT 'My JobID is  ' + Convert(char(255), @JobID)  

现在您使用 @JobID 进行处理,但据我所知,到目前为止您必须始终转换为 char(255)。
感谢 MSDN

thanks for your answers. The problem is that I tried to parse the statement in job step. Then I got this error. While running the job there's no problem.
My very best solution now is:

declare @JobID uniqueidentifier
SELECT @JobID = $(ESCAPE_NONE(JOBID));
PRINT 'My JobID is  ' + Convert(char(255), @JobID)  

Now you handle with @JobID, but as far as I know until now you have to convert always to char(255).
Thanks to user state_dba on MSDN.

〆一缕阳光ご 2024-10-10 07:07:04

只是忘记解析器所说的——变量解析是在运行时完成的。解析器不知道这一点。

Just forget what parser is saying - variable resolution is done at runtime. Parser does not know about that.

许仙没带伞 2024-10-10 07:07:04

这听起来可能是显而易见的,但我得到了您从第一个示例中引用的错误,如果我在查询窗口中运行它,但当我将该脚本粘贴到作业步骤中时它运行得非常好。

您只能在作业步骤中使用这些令牌。而且,考虑到我们不希望 jobid 令牌中出现任何引号,因此每当您引用它时我都会使用 ESCAPE_NONE 。

This may sound obvious, but I get the error you quoted from your first sample, if I run it in a query window, but it runs perfectly well when I paste that script into a job step.

You can only use these tokens within job steps. And, given that we're not expecting any quotes in the jobid token, I'd use ESCAPE_NONE whenever you reference it.

谁把谁当真 2024-10-10 07:07:04

对于那些需要替代方法来获取自己的作业 ID(无需宏)的用户,例如,从作业步骤调用的存储过程中获取作业 ID。我在此处找到了以下内容

DECLARE @SQL NVARCHAR(72),
@jobID UNIQUEIDENTIFIER,
@jobName SYSNAME

IF SUBSTRING(APP_NAME(),1,8) = 'SQLAgent'
BEGIN 
    SET @SQL = 'SET @guid = CAST(' + SUBSTRING(APP_NAME(), 30, 34) + ' AS UNIQUEIDENTIFIER)'

    EXEC    sp_executesql @SQL, N'@guid UNIQUEIDENTIFIER OUT', @guid = @jobID OUT

    SELECT  @jobName = name
    FROM    msdb..sysjobs
    WHERE   job_id = @jobID
END

For those that need an alternative method to get your own job ID without macros, for example, from within a stored procedure that a job step calls. I found the following here

DECLARE @SQL NVARCHAR(72),
@jobID UNIQUEIDENTIFIER,
@jobName SYSNAME

IF SUBSTRING(APP_NAME(),1,8) = 'SQLAgent'
BEGIN 
    SET @SQL = 'SET @guid = CAST(' + SUBSTRING(APP_NAME(), 30, 34) + ' AS UNIQUEIDENTIFIER)'

    EXEC    sp_executesql @SQL, N'@guid UNIQUEIDENTIFIER OUT', @guid = @jobID OUT

    SELECT  @jobName = name
    FROM    msdb..sysjobs
    WHERE   job_id = @jobID
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文