SQL Server 代理作业:如何执行作业步骤而不执行整个作业

发布于 2024-11-16 18:43:54 字数 287 浏览 2 评论 0原文

我有一个 SQL Server 代理作业,以前有两个步骤。今天,我必须集成第三步,很快我就需要集成第四步。

我想确保该步骤能够正确执行,但我不想执行整个作业。

前两个步骤需要相当多的时间来执行,并且在白天它们会占用用户所需的大量 SQL 资源。

有没有办法可以执行作业步骤而不是整个作业流程?

在 SSMS 中,如果我右键单击作业,则会有一个选项显示“在步骤中启动作业...”,除非我尝试该选项时,它会弹出一个对话框,似乎暗示整个作业已启动。我可以做什么来测试工作中的一个步骤?

提前致谢。

I have a SQL Server Agent Job that previously had two steps. Today, I've had to integrate the third step, and soon I'll need to integrate a fourth.

I want to be sure that the step will execute properly but I do not want to execute the entire job.

The first two steps take quite a bit of time to execute and during the day they hog a significant amount of the SQL resources that my users need.

Is there a way that I can execute a job step and not an entire job process?

Within SSMS, if I right-click on the job there is an option that states "Start Job at step..." except when I try that option it brings up a dialog that seems to imply that the entire job has been started. What can I do to test one step within a job?

Thanks in advance.

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

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

发布评论

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

评论(6

爱她像谁 2024-11-23 18:43:54

“在步骤开始作业”将在您指定的步骤开始作业。但是 - 如果您不想执行任何后续步骤 - 请务必调整步骤逻辑,以便在完成您开始的步骤后它将“退出报告成功”。

"Start job at step" will start the job at the step you specify. However - if you don't wish to execute any subsequent steps - be sure to adjust the step logic so that it will "Quit reporting success" after completing the step you started at.

野の 2024-11-23 18:43:54

在 SSMS 中:

  • 从作业步骤复制代码(使用 ctrl + a 选择全部)
  • 将代码粘贴到 SSMS 中的新查询窗口中
  • 运行代码

如果您经常需要运行几个步骤,则可以交替执行。

将这些步骤放在一个单独的作业中,然后从另一个作业中启动一个作业来运行所有内容。使用 EXEC msdb.dbo.sp_start_job N'job_name'

您可以按需运行简短的简单作业,并按计划运行完整的长作业

In SSMS:

  • Copy the code from the job step (use ctrl + a to select all)
  • Paste the code in to a new query window in SSMS
  • Run the code

Alternately if you have a recurring need to run just a couple steps.

Put those steps in a separate job, then kick off one job from the other to run everything. Use EXEC msdb.dbo.sp_start_job N'job_name'

You can run the short easy job on demand, and the full long job as scheduled

温馨耳语 2024-11-23 18:43:54

使用这个:

EXEC msdb.dbo.sp_start_job N'job_name' , 'step_name'

Use this:

EXEC msdb.dbo.sp_start_job N'job_name' , 'step_name'
木落 2024-11-23 18:43:54

--如何仅运行作业的一个步骤

基本上我保存了作业中该步骤的当前情况 - 特别是成功时做什么以及失败时做什么。

然后我改变它以相应地结束工作的成功或失败。

这是脚本:

USE [msdb]
GO

declare @on_success_action int,
        @on_fail_action int,
        @run_the_job bit = 0

if object_id('tempdb..#the_only_step',N'U') is not null
   drop table #the_only_step

create table #the_only_step(
step_id int,--  Unique identifier for the step.
step_name   sysname,--  Name of the step in the job.
subsystem   nvarchar(40),-- Subsystem in which to execute the step command.
command nvarchar(max),--    Command executed in the step.
flags   int,--  A bitmask of values that control step behavior.
cmdexec_success_code    int,--  For a CmdExec step, this is the process exit code of a successful command.
on_success_action   tinyint,--  Action to take if the step succeeds:

--1 = Quit the job reporting success.
--2 = Quit the job reporting failure.
--3 = Go to the next step.
--4 = Go to step.

on_success_step_id  int,--  If on_success_action is 4, this indicates the next step to execute.
on_fail_action  tinyint,--  What to do if the step fails. Values are same as on_success_action.
on_fail_step_id int,--  If on_fail_action is 4, this indicates the next step to execute.
server  varchar(100) null,--    Reserved.
database_name   varchar(100) null,--    For a Transact-SQL step, this is the database in which the command executes.
database_user_name  varchar(100) null,--    For a Transact-SQL step, this is the database user context in which the command executes.
retry_attempts  int,--  Maximum number of times the command should be retried (if it is unsuccessful).
retry_interval  int,--  Interval (in minutes) for any retry attempts.
os_run_priority int,--  Reserved.
output_file_name    nvarchar(200),--    File to which command output should be written (Transact-SQL, CmdExec, and PowerShell steps only).
last_run_outcome    int,--  Outcome of the step the last time it ran:

--0 = Failed
--1 = Succeeded
--2 = Retry
--3 = Canceled
--5 = Unknown
last_run_duration   int,--  Duration (hhmmss) of the step the last time it ran.
last_run_retries    int,--  Number of times the command was retried the last time the step ran.
last_run_date   int,--  Date the step last started execution.
last_run_time   int,--  Time the step last started execution.
proxy_id    int
)

insert into #the_only_step
EXEC msdb.dbo.sp_help_jobstep 
        @job_name=N'SUPT_munichRE - after restore', 
        @step_id=2


--you can check if things are there as expected
select * from #the_only_step


--we want to change the step:
--after finished - success and after finished - failure - either way end the job




select @on_success_action= a.on_success_action,
       @on_fail_action = a.on_fail_action
from #the_only_step a

EXEC msdb.dbo.sp_update_jobstep 
        @job_name=N'SUPT_munichRE - after restore', 
        @step_id=2 , 
        @on_success_action=1, 
        @on_fail_action=2

if (@run_the_job = 1) 
   BEGIN

                EXEC msdb.dbo.sp_start_job 
                @job_name=N'SUPT_munichRE - after restore',
                @step_name = N'2'

                PRINT 'job started'

   END
 
 --after it finishes running
  --set the values back as they originally were


  EXEC msdb.dbo.sp_update_jobstep 
        @job_name=N'SUPT_munichRE - after restore', 
        @step_id=2 , 
        @on_success_action=@on_success_action, 
        @on_fail_action=@on_fail_action

现在另一个有趣的点是

开始工作后,如何等到它完成后再继续?

这里回答了这个问题:

--[有没有办法多次运行同一个作业而不需要调度它?][1]

-- INSERT EXEC 语句不能嵌套。
--http://stackoverflow.com/questions/3795263/errors-insert-exec-statement-cannot-be-nested-and-cannot-use-the-rollback-s

基本上我使用该存储过程来运行作业msdb.dbo.sp_start_job 如上面的脚本所示。

希望这有帮助
因为它对我非常有用!

--how to run only a step of a job

basically I save the current situation of the step within a job - particularly what to do when success and what to do if fails.

then I change it to end the job in success or failure accordingly.

here is the script:

USE [msdb]
GO

declare @on_success_action int,
        @on_fail_action int,
        @run_the_job bit = 0

if object_id('tempdb..#the_only_step',N'U') is not null
   drop table #the_only_step

create table #the_only_step(
step_id int,--  Unique identifier for the step.
step_name   sysname,--  Name of the step in the job.
subsystem   nvarchar(40),-- Subsystem in which to execute the step command.
command nvarchar(max),--    Command executed in the step.
flags   int,--  A bitmask of values that control step behavior.
cmdexec_success_code    int,--  For a CmdExec step, this is the process exit code of a successful command.
on_success_action   tinyint,--  Action to take if the step succeeds:

--1 = Quit the job reporting success.
--2 = Quit the job reporting failure.
--3 = Go to the next step.
--4 = Go to step.

on_success_step_id  int,--  If on_success_action is 4, this indicates the next step to execute.
on_fail_action  tinyint,--  What to do if the step fails. Values are same as on_success_action.
on_fail_step_id int,--  If on_fail_action is 4, this indicates the next step to execute.
server  varchar(100) null,--    Reserved.
database_name   varchar(100) null,--    For a Transact-SQL step, this is the database in which the command executes.
database_user_name  varchar(100) null,--    For a Transact-SQL step, this is the database user context in which the command executes.
retry_attempts  int,--  Maximum number of times the command should be retried (if it is unsuccessful).
retry_interval  int,--  Interval (in minutes) for any retry attempts.
os_run_priority int,--  Reserved.
output_file_name    nvarchar(200),--    File to which command output should be written (Transact-SQL, CmdExec, and PowerShell steps only).
last_run_outcome    int,--  Outcome of the step the last time it ran:

--0 = Failed
--1 = Succeeded
--2 = Retry
--3 = Canceled
--5 = Unknown
last_run_duration   int,--  Duration (hhmmss) of the step the last time it ran.
last_run_retries    int,--  Number of times the command was retried the last time the step ran.
last_run_date   int,--  Date the step last started execution.
last_run_time   int,--  Time the step last started execution.
proxy_id    int
)

insert into #the_only_step
EXEC msdb.dbo.sp_help_jobstep 
        @job_name=N'SUPT_munichRE - after restore', 
        @step_id=2


--you can check if things are there as expected
select * from #the_only_step


--we want to change the step:
--after finished - success and after finished - failure - either way end the job




select @on_success_action= a.on_success_action,
       @on_fail_action = a.on_fail_action
from #the_only_step a

EXEC msdb.dbo.sp_update_jobstep 
        @job_name=N'SUPT_munichRE - after restore', 
        @step_id=2 , 
        @on_success_action=1, 
        @on_fail_action=2

if (@run_the_job = 1) 
   BEGIN

                EXEC msdb.dbo.sp_start_job 
                @job_name=N'SUPT_munichRE - after restore',
                @step_name = N'2'

                PRINT 'job started'

   END
 
 --after it finishes running
  --set the values back as they originally were


  EXEC msdb.dbo.sp_update_jobstep 
        @job_name=N'SUPT_munichRE - after restore', 
        @step_id=2 , 
        @on_success_action=@on_success_action, 
        @on_fail_action=@on_fail_action

Now another interesting point is

after starting the job, how to wait until it is finished before moving on?

that is answered here:

--[is there a way to run the same job multiple times without scheduling it?][1]

--An INSERT EXEC statement cannot be nested.
--http://stackoverflow.com/questions/3795263/errors-insert-exec-statement-cannot-be-nested-and-cannot-use-the-rollback-s

Basically I use that stored procedure to run the job instead of msdb.dbo.sp_start_job as in the script above.

hope this helps
as it is very useful to me!

北方。的韩爷 2024-11-23 18:43:54

转到作业属性并手动执行该步骤。例如,有一个作业有 16 个步骤,您想要运行第 12 个步骤,然后转到该作业属性并查看第 12 步到底做了什么。如果它在命令提示符下执行批处理文件,请手动执行。但这个逻辑并不能适用于所有情况。

Go to job properties and manually do that step. For example, there is a job with 16 steps and you want to run the 12th step, then go to that job property and see what that step 12 exactly does. If it executes a batch file in command prompt, do that manually. But this logic cannot be used in all cases.

a√萤火虫的光℡ 2024-11-23 18:43:54
USE [**DB_NAME**]
GO

DECLARE @return_value int

EXEC @return_value = [dbo].[**STEP_NAME**]  

SELECT 'Return Value' = @return_value
GO
USE [**DB_NAME**]
GO

DECLARE @return_value int

EXEC @return_value = [dbo].[**STEP_NAME**]  

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