Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Ottieni lo stato del processo di SQL Server

Non così difficile. Tieni presente che SQL Server conserva sempre le informazioni sui metadati sui dettagli che vengono eseguiti attivamente sul server. Lo farei per trovare errori in quanto potresti creare un processo o una funzione, chiamarlo per intervallo di date. Ho fornito un esempio solo della query per il momento:

use msdb;

declare
    @Start int =  cast( convert(varchar,  
            dateadd(ww, datediff(ww, 0, getdate())-1,0)  -- last week starting
            , 112) as int)
,   @End int = cast( convert(varchar,
            getdate()  -- current datetime
            , 112) as int)
;

Select 
    j.name
,   j.description
,   cast( cast(jh.run_date as varchar) + ' ' +  left(jh.run_time, 2) + ':' + substring( cast(jh.run_time as varchar), 3, 2) as datetime) as TimeRan
,   jh.message
,   jh.step_id
,   jh.step_name
from sysjobs j (nolock) 
    join sysjobhistory jh (nolock) on j.job_id = jh.job_id
        and jh.run_date between @Start and @End
        and jh.run_status = 0  -- 0 is failure