AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / dba / Perguntas / 14290
Accepted
u23432534
u23432534
Asked: 2012-03-03 09:15:48 +0800 CST2012-03-03 09:15:48 +0800 CST 2012-03-03 09:15:48 +0800 CST

Obter trabalho de agente do SQL Server com falha?

  • 772

Como obter uma lista de trabalhos com falha executados na noite passada? Eu só encontro o seguinte script powershell. Qual é a equivalência SQL?

dir $psPath\Jobs | % { $_.EnumHistory() } | ? { $_.RunStatus -ne 1 }
sql-server sql-server-2008
  • 2 2 respostas
  • 4237 Views

2 respostas

  • Voted
  1. Eric Higgins
    2012-03-03T09:26:37+08:002012-03-03T09:26:37+08:00

    Aqui está. De: http://www.toadworld.com/platforms/sql-server/w/wiki/10351.sql-server-agent-job-query-samples.aspx

    SET  NOCOUNT ON
    
    DECLARE @MaxLength   INT
    SET @MaxLength   = 50
    
    DECLARE @xp_results TABLE (
                           job_id uniqueidentifier NOT NULL,
                           last_run_date nvarchar (20) NOT NULL,
                           last_run_time nvarchar (20) NOT NULL,
                           next_run_date nvarchar (20) NOT NULL,
                           next_run_time nvarchar (20) 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
                        )
    
    DECLARE @job_owner   sysname
    
    DECLARE @is_sysadmin   INT
    SET @is_sysadmin   = isnull (is_srvrolemember ('sysadmin'), 0)
    SET @job_owner   = suser_sname ()
    
    INSERT INTO @xp_results
       EXECUTE sys.xp_sqlagent_enum_jobs @is_sysadmin, @job_owner
    
    UPDATE @xp_results
       SET last_run_time    = right ('000000' + last_run_time, 6),
           next_run_time    = right ('000000' + next_run_time, 6)
    
    SELECT j.name AS JobName,
           j.enabled AS Enabled,
           CASE x.running
              WHEN 1
              THEN
                 'Running'
              ELSE
                 CASE h.run_status
                    WHEN 2 THEN 'Inactive'
                    WHEN 4 THEN 'Inactive'
                    ELSE 'Completed'
                 END
           END
              AS CurrentStatus,
           coalesce (x.current_step, 0) AS CurrentStepNbr,
           CASE
              WHEN x.last_run_date > 0
              THEN
                 convert (datetime,
                            substring (x.last_run_date, 1, 4)
                          + '-'
                          + substring (x.last_run_date, 5, 2)
                          + '-'
                          + substring (x.last_run_date, 7, 2)
                          + ' '
                          + substring (x.last_run_time, 1, 2)
                          + ':'
                          + substring (x.last_run_time, 3, 2)
                          + ':'
                          + substring (x.last_run_time, 5, 2)
                          + '.000',
                          121
                 )
              ELSE
                 NULL
           END
              AS LastRunTime,
           CASE h.run_status
              WHEN 0 THEN 'Fail'
              WHEN 1 THEN 'Success'
              WHEN 2 THEN 'Retry'
              WHEN 3 THEN 'Cancel'
              WHEN 4 THEN 'In progress'
           END
              AS LastRunOutcome,
           CASE
              WHEN h.run_duration > 0
              THEN
                   (h.run_duration / 1000000) * (3600 * 24)
                 + (h.run_duration / 10000 % 100) * 3600
                 + (h.run_duration / 100 % 100) * 60
                 + (h.run_duration % 100)
              ELSE
                 NULL
           END
              AS LastRunDuration
      FROM          @xp_results x
                 LEFT JOIN
                    msdb.dbo.sysjobs j
                 ON x.job_id = j.job_id
              LEFT OUTER JOIN
                 msdb.dbo.syscategories c
              ON j.category_id = c.category_id
           LEFT OUTER JOIN
              msdb.dbo.sysjobhistory h
           ON     x.job_id = h.job_id
              AND x.last_run_date = h.run_date
              AND x.last_run_time = h.run_time
              AND h.step_id = 0
    
    • 4
  2. Best Answer
    datagod
    2012-03-03T10:17:26+08:002012-03-03T10:17:26+08:00

    Aqui está um proc que fará o truque:

    IF (object_id('ShowJobHistory') IS NOT NULL)
    BEGIN
      print 'Dropping procedure: ShowJobHistory'
      drop procedure ShowJobHistory
    END
    print 'Creating procedure: ShowJobHistory'
    GO
    CREATE PROCEDURE ShowJobHistory
    @Days int = 14
    as
    ---------------------------------------------------------------------------------------------------
    -- Date Created: September 13, 2010
    -- Author:       Bill McEvoy
    -- Description:  This procedure produces an easy to read report that details all jobs
    --               that have run on this server in the specified number of days.
    --               
    ---------------------------------------------------------------------------------------------------
    -- Date Revised: 
    -- Author:       
    -- Reason:       
    ---------------------------------------------------------------------------------------------------
    set nocount on
    
    ---------------------------------------------------------------------
    -- Generate report                                                 --
    ---------------------------------------------------------------------
    
    
    print ' '
    print ' '
    print 'RECENT JOB HISTORY'
    print '=================='
    print ' '
    
    select 'ID'        = convert(char(8), h.instance_id),
           'Run Time'  = convert(char(10),convert(datetime,convert(char(8),h.run_date)),120) + ' ' + left(right('00000' + cast(run_time as varchar(6)),6),2) + ':' + left(right('00000' + cast(run_time as varchar(6)),4),2) + ':' +  right(cast(run_time as varchar(6)),2) + ' ',
           'Duration'  = case when (h.run_duration > 1800) then '>' else ' ' end + left(right(convert(char(19),(dateadd(ss,h.run_duration,'')),20),8),8) + ' ',
           'Status'    = case(run_status)
                           when 0 then '** FAILED ** '
                           when 1 then 'Success '
                           when 2 then 'RETRY '
                           when 3 then 'CANCELLED '
                           when 4 then 'IN PROGRESS '
                           else '??'
                         end,
           'Step'      = convert(char(3),h.step_id),
           'Job Name'  = left(s.name,50),
           'Step Name' = left(h.step_name,35),
           'Message'   = left(h.message,200)
      from msdb.dbo.sysjobhistory h
     right join msdb.dbo.sysjobs s on s.job_id = h.job_id
     where h.run_date >= convert(int,(convert(char(8), (getdate()-@Days),112)))
    --   and (h.step_id = 0 or run_status = 0)
     order by h.instance_id desc
    
    
    
    go
    IF (object_id('ShowJobHistory') IS NOT NULL)
      print 'Procedure created'
    ELSE
      print 'Procedure NOT created'
    GO
    
    • 2

relate perguntas

  • Quais são as principais causas de deadlocks e podem ser evitadas?

  • Quanto "Padding" coloco em meus índices?

  • Existe um processo do tipo "práticas recomendadas" para os desenvolvedores seguirem para alterações no banco de dados?

  • Como determinar se um Índice é necessário ou necessário

  • Downgrade do SQL Server 2008 para 2005

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    Como ver a lista de bancos de dados no Oracle?

    • 8 respostas
  • Marko Smith

    Quão grande deve ser o mysql innodb_buffer_pool_size?

    • 4 respostas
  • Marko Smith

    Listar todas as colunas de uma tabela especificada

    • 5 respostas
  • Marko Smith

    restaurar a tabela do arquivo .frm e .ibd?

    • 10 respostas
  • Marko Smith

    Como usar o sqlplus para se conectar a um banco de dados Oracle localizado em outro host sem modificar meu próprio tnsnames.ora

    • 4 respostas
  • Marko Smith

    Como você mysqldump tabela (s) específica (s)?

    • 4 respostas
  • Marko Smith

    Como selecionar a primeira linha de cada grupo?

    • 6 respostas
  • Marko Smith

    Listar os privilégios do banco de dados usando o psql

    • 10 respostas
  • Marko Smith

    Como inserir valores em uma tabela de uma consulta de seleção no PostgreSQL?

    • 4 respostas
  • Marko Smith

    Como faço para listar todos os bancos de dados e tabelas usando o psql?

    • 7 respostas
  • Martin Hope
    Mike Walsh Por que o log de transações continua crescendo ou fica sem espaço? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland Listar todas as colunas de uma tabela especificada 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney O MySQL pode realizar consultas razoavelmente em bilhões de linhas? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx Como posso monitorar o andamento de uma importação de um arquivo .sql grande? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison Como você mysqldump tabela (s) específica (s)? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    pedrosanta Listar os privilégios do banco de dados usando o psql 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas Como posso cronometrar consultas SQL usando psql? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas Como inserir valores em uma tabela de uma consulta de seleção no PostgreSQL? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas Como faço para listar todos os bancos de dados e tabelas usando o psql? 2011-02-18 00:45:49 +0800 CST
  • Martin Hope
    bernd_k Quando devo usar uma restrição exclusiva em vez de um índice exclusivo? 2011-01-05 02:32:27 +0800 CST

Hot tag

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve