sp_procoption没有在存储过程下在MSDB上执行连接查询

时间:2017-02-10 06:18:03

标签: sql sql-server tsql optimization

我正在使用sp_procoption来提取失败的作业详细信息和备份信息 但是当通过sp_procoption执行时它没有生成任何结果,但是当我手动执行该过程时,它的工作正常。

它是否因为我在代码中使用的连接而发生,即使我在插入之前使用waitfor延迟为sql server准备好一些秒,但没有运气。我真的很想知道解决这个问题背后的原因。

Use master
go
CREATE PROCEDURE  [dbo].[job details] 
@JobName NVARCHAR (1000)
AS
IF OBJECT_ID('TempDB..#JobResults','U') IS NOT NULL DROP TABLE #JobResults
CREATE TABLE #JobResults ( Job_ID   UNIQUEIDENTIFIER NOT NULL, 
                                   Last_Run_Date         INT NOT NULL, 
                                   Last_Run_Time         INT NOT NULL, 
                                   Next_Run_date         INT NOT NULL, 
                                   Next_Run_Time         INT NOT NULL, 
                                   Next_Run_Schedule_ID  INT NOT NULL, 
                                   Requested_to_Run      INT NOT NULL,
                                   Request_Source        INT NOT NULL, 
                                   Request_Source_id     SYSNAME 
                                   COLLATE Database_Default      NULL, 
                                   Running               INT NOT NULL,
                                   Current_Step          INT NOT NULL, 
                                   Current_Retry_Attempt INT NOT NULL, 
                                   Job_State             INT NOT NULL ) 
INSERT  #JobResults 
        EXECUTE master.dbo.xp_sqlagent_enum_jobs 1, '';

        SELECT  job.name                                                AS [Job_Name], 
              ( SELECT  MAX(CAST( STUFF(STUFF(CAST(jh.run_date AS VARCHAR),7,0,'-'),5,0,'-') + ' ' + 
                        STUFF(STUFF(REPLACE(STR(jh.run_time,6,0),' ','0'),5,0,':'),3,0,':') AS DATETIME))
                FROM    msdb.dbo.sysjobs AS j 
                    INNER JOIN msdb.dbo.sysjobhistory AS jh 
                       ON jh.job_id = j.job_id AND jh.step_id = 0 
                WHERE j.[name] LIKE '%' + @JobName + '%' 
                GROUP BY j.[name] )                                     AS [Last_Completed_DateTime], 
              ( SELECT  TOP 1 start_execution_date 
                FROM    msdb.dbo.sysjobactivity
                WHERE   job_id = r.job_id
                ORDER BY start_execution_date DESC )                    AS [Job_Start_DateTime],
            CASE 
                WHEN r.running = 0 THEN
                    CASE 
                        WHEN jobInfo.lASt_run_outcome = 0 THEN 'Failed'
                        WHEN jobInfo.lASt_run_outcome = 1 THEN 'Success'
                        WHEN jobInfo.lASt_run_outcome = 3 THEN 'Canceled'
                        ELSE 'Unknown'
                    END
                        WHEN r.job_state = 0 THEN 'Success'
                        WHEN r.job_state = 4 THEN 'Success'
                        WHEN r.job_state = 5 THEN 'Success'
                        WHEN r.job_state = 1 THEN 'In Progress'
                        WHEN r.job_state = 2 THEN 'In Progress'
                        WHEN r.job_state = 3 THEN 'In Progress'
                        WHEN r.job_state = 7 THEN 'In Progress'
                     ELSE 'Unknown' END                                 AS [Run_Status_Description]
        FROM    #JobResults AS r 
            LEFT OUTER JOIN msdb.dbo.sysjobservers AS jobInfo 
               ON r.job_id = jobInfo.job_id 
            INNER JOIN msdb.dbo.sysjobs AS job 
               ON r.job_id = job.job_id 
        WHERE   job.[enabled] = 1
                AND job.name LIKE '%' + @JobName + '%'
GO

1 个答案:

答案 0 :(得分:0)

启动过程不能包含INPUTOUTPUT个参数。列出的过程需要输入参数@JobName。尝试创建没有参数的存储过程,也可以使用默认值。