<i id='m8xOl'><tr id='m8xOl'><dt id='m8xOl'><q id='m8xOl'><span id='m8xOl'><b id='m8xOl'><form id='m8xOl'><ins id='m8xOl'></ins><ul id='m8xOl'></ul><sub id='m8xOl'></sub></form><legend id='m8xOl'></legend><bdo id='m8xOl'><pre id='m8xOl'><center id='m8xOl'></center></pre></bdo></b><th id='m8xOl'></th></span></q></dt></tr></i><div id='m8xOl'><tfoot id='m8xOl'></tfoot><dl id='m8xOl'><fieldset id='m8xOl'></fieldset></dl></div>
    1. <small id='m8xOl'></small><noframes id='m8xOl'>

      <tfoot id='m8xOl'></tfoot>
        <bdo id='m8xOl'></bdo><ul id='m8xOl'></ul>

      <legend id='m8xOl'><style id='m8xOl'><dir id='m8xOl'><q id='m8xOl'></q></dir></style></legend>

      从存储过程执行 SQL Server 代理作业并返回作业结果

      Executing SQL Server Agent Job from a stored procedure and returning job result(从存储过程执行 SQL Server 代理作业并返回作业结果)

        <i id='q1HPH'><tr id='q1HPH'><dt id='q1HPH'><q id='q1HPH'><span id='q1HPH'><b id='q1HPH'><form id='q1HPH'><ins id='q1HPH'></ins><ul id='q1HPH'></ul><sub id='q1HPH'></sub></form><legend id='q1HPH'></legend><bdo id='q1HPH'><pre id='q1HPH'><center id='q1HPH'></center></pre></bdo></b><th id='q1HPH'></th></span></q></dt></tr></i><div id='q1HPH'><tfoot id='q1HPH'></tfoot><dl id='q1HPH'><fieldset id='q1HPH'></fieldset></dl></div>

          • <bdo id='q1HPH'></bdo><ul id='q1HPH'></ul>
              <tfoot id='q1HPH'></tfoot>
            • <small id='q1HPH'></small><noframes id='q1HPH'>

                  <tbody id='q1HPH'></tbody>
                <legend id='q1HPH'><style id='q1HPH'><dir id='q1HPH'><q id='q1HPH'></q></dir></style></legend>

                本文介绍了从存储过程执行 SQL Server 代理作业并返回作业结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                需要有一个存储过程来调用 SQL Server 代理作业并返回该作业是否成功运行.

                Need to have a stored procedure that calls a SQL Server Agent Job and returns whether or not the job ran successfully or not.

                到目前为止我有

                So far I have

                CREATE PROCEDURE MonthlyData
                AS
                EXEC msdb.dbo.sp_start_job N'MonthlyData'
                
                WAITFOR DELAY '000:04:00'
                
                EXEC msdb.dbo.sp_help_jobhistory @job_name = 'MonthlyData'
                GO
                

                哪个开始工作,如果工作成功与否,返回的最佳方法是什么?

                Which starts the job, whats the best way to get back if the job ran successfully or not?

                Ok 进行了编辑并使用了 WAITFOR DELAY,因为该作业通常在 3-4 分钟之间运行,从不超过 4 分钟.该作业是否有更有效的方法来完成?

                Ok made an edit and used WAITFOR DELAY as the job normally runs between 3-4 mins never longer than 4. Does the job but is there a more efficient way to do it?

                推荐答案

                您可以运行查询:

                EXEC msdb.dbo.sp_help_jobhistory 
                    @job_name = N'MonthlyData'
                

                它将返回一列 run_status.状态为:

                It'll return a column run_status. Statuses are:

                 0 - Failed
                 1 - Succeeded
                 2 - Retry
                 3 - Canceled         
                

                有关 MSDN

                编辑:您可能想要轮询您的工作并确保它已执行.您可以从 sp_help_job 过程中获取此信息.当此过程返回4 状态时,表示作业空闲.然后检查它的运行状态是安全的.

                EDIT: You might want to to poll your job and make sure it's executed. You can get this information from sp_help_job procedure. When this procedure returns status of 4 it means the job is idle. Then it's safe to check for it's run status.

                您可以使用以下代码进行投票:

                You can poll using following code:

                DECLARE @job_status INT
                SELECT @job_status = current_execution_status FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes;','exec msdb.dbo.sp_help_job @job_name = ''NightlyBackups''')
                
                WHILE @job_status <> 4
                BEGIN
                    WAITFOR DELAY '00:00:03'
                    SELECT @job_status = current_execution_status FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes;','exec msdb.dbo.sp_help_job @job_name = ''NightlyBackups''')
                END
                
                EXEC msdb.dbo.sp_help_jobhistory 
                    @job_name = N'NightlyBackups' ;
                GO
                

                此代码将检查状态,等待 3 秒钟,然后重试.一旦状态为 4,我们就知道工作已完成,可以安全地检查工作历史记录.

                This code will check for the status, wait for 3 seconds and try again. Once we get status of 4 we know the job is done and it's safe to check for the job history.

                这篇关于从存储过程执行 SQL Server 代理作业并返回作业结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Building a comma separated list?(建立一个逗号分隔的列表?)
                Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)

                  <bdo id='FS7qo'></bdo><ul id='FS7qo'></ul>
                  <i id='FS7qo'><tr id='FS7qo'><dt id='FS7qo'><q id='FS7qo'><span id='FS7qo'><b id='FS7qo'><form id='FS7qo'><ins id='FS7qo'></ins><ul id='FS7qo'></ul><sub id='FS7qo'></sub></form><legend id='FS7qo'></legend><bdo id='FS7qo'><pre id='FS7qo'><center id='FS7qo'></center></pre></bdo></b><th id='FS7qo'></th></span></q></dt></tr></i><div id='FS7qo'><tfoot id='FS7qo'></tfoot><dl id='FS7qo'><fieldset id='FS7qo'></fieldset></dl></div>
                • <tfoot id='FS7qo'></tfoot>

                      <tbody id='FS7qo'></tbody>

                    <small id='FS7qo'></small><noframes id='FS7qo'>

                        • <legend id='FS7qo'><style id='FS7qo'><dir id='FS7qo'><q id='FS7qo'></q></dir></style></legend>