SQL Server 代理 - 获取我自己的 job_id
我正在运行安装了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我们最近在这方面遇到了麻烦,并且没有遵循您在 MSDN 中找到的路线。相反,我们直接按名称从 dbo.sysjobs 恢复 jobid(与您的示例相反),然后在作业中使用它来检查执行状态(如果作业状态发生更改,则退出长时间运行的 while 循环)。
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).
感谢您的回答。问题是我试图解析作业步骤中的语句。然后我得到了这个错误。运行作业时没有问题。
我现在最好的解决方案是:
现在您使用 @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:
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.
只是忘记解析器所说的——变量解析是在运行时完成的。解析器不知道这一点。
Just forget what parser is saying - variable resolution is done at runtime. Parser does not know about that.
这听起来可能是显而易见的,但我得到了您从第一个示例中引用的错误,如果我在查询窗口中运行它,但当我将该脚本粘贴到作业步骤中时它运行得非常好。
您只能在作业步骤中使用这些令牌。而且,考虑到我们不希望 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.
对于那些需要替代方法来获取自己的作业 ID(无需宏)的用户,例如,从作业步骤调用的存储过程中获取作业 ID。我在此处找到了以下内容
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