SQL Server代理-获得我自己的job_id [英] SQL Server Agent - get my own job_id

查看:276
本文介绍了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))之类的宏.没问题.
但是,如果您尝试该示例:

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

您得到:
'ESCAPE_SQUOTE'附近的语法不正确. (Microsoft SQL Server,错误:102)
好的,现在从头开始:

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

PRINT N'$(ESCAPE_SQUOTE(JOBID))'  

结果为0xE33FE637C10B3C49A6E958BB3EF06959,但job_id为 37E63FE3-0BC1-493C-A6E9-58BB3EF06959
我认为"N"会隐式转换为(JOBID)的NVARCHAR ...
好的,我想我必须关心(JOBID)的数据类型.在第168/169页的"SQL Server 2008管理"一书中,还有一个使用(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)))

结果:
'('附近的语法不正确.(Microsoft SQL Server,错误:102)
我现在很困惑.可以请别人帮我一个好的建议或解决方案.各种帮助都值得赞赏.

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.

最诚挚的问候 赫尔穆特(Helmut)

Best regards Helmut

推荐答案

忘了解析器在说什么-变量解析是在运行时完成的.解析器对此一无所知.

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

这篇关于SQL Server代理-获得我自己的job_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆