Skip to content

Commit d65f497

Browse files
Merge pull request #21734 from LitKnd/sysjobhistory
Refreshing sysjobhistory
2 parents 69571eb + 9851c8d commit d65f497

1 file changed

Lines changed: 33 additions & 21 deletions

File tree

docs/relational-databases/system-tables/dbo-sysjobhistory-transact-sql.md

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
description: "dbo.sysjobhistory (Transact-SQL)"
3-
title: "dbo.sysjobhistory (Transact-SQL) | Microsoft Docs"
3+
title: "dbo.sysjobhistory (Transact-SQL)"
44
ms.custom: ""
5-
ms.date: "06/24/2019"
5+
ms.date: "04/18/2022"
66
ms.prod: sql
77
ms.prod_service: "database-engine"
88
ms.reviewer: ""
@@ -22,9 +22,9 @@ author: LitKnd
2222
ms.author: kendralittle
2323
---
2424
# dbo.sysjobhistory (Transact-SQL)
25-
[!INCLUDE [SQL Server](../../includes/applies-to-version/sqlserver.md)]
25+
[!INCLUDE [SQL Server SQL MI](../../includes/applies-to-version/sql-asdbmi.md)]
2626

27-
Contains information about the execution of scheduled jobs by [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] Agent.
27+
Contains information about the execution of scheduled jobs by the [SQL Server Agent](../../ssms/agent/sql-server-agent.md).
2828

2929
> [!NOTE]
3030
> In most cases the data is updated only after the job step completes and the table typically contains no records for job steps that are currently in progress, but in some cases underlying processes *do* provide information about in progress job steps.
@@ -43,31 +43,43 @@ This table is stored in the **msdb** database.
4343
|**run_status**|**int**|Status of the job execution:<br /><br /> **0** = Failed<br /><br /> **1** = Succeeded<br /><br /> **2** = Retry<br /><br /> **3** = Canceled<br /><br />**4** = In Progress|
4444
|**run_date**|**int**|Date the job or step started execution. For an In Progress history, this is the date/time the history was written.|
4545
|**run_time**|**int**|Time the job or step started in **HHMMSS** format.|
46-
|**run_duration**|**int**|Elapsed time in the execution of the job or step in **HHMMSS** format.|
46+
|**run_duration**|**int**|Elapsed time in the execution of the job or step in **HHMMSS** format for time periods up to 24 hours. Find code to translate longer run durations in [Example](#example).|
4747
|**operator_id_emailed**|**int**|ID of the operator notified when the job completed.|
4848
|**operator_id_netsent**|**int**|ID of the operator notified by a message when the job completed.|
4949
|**operator_id_paged**|**int**|ID of the operator notified by pager when the job completed.|
5050
|**retries_attempted**|**int**|Number of retry attempts for the job or step.|
5151
|**server**|**sysname**|Name of the server where the job was executed.|
5252

53-
## Example
54-
The following [!INCLUDE[tsql](../../includes/tsql-md.md)] query will convert the **run_time** and **run_duration** columns into a more user friendly format. Execute the script in [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)].
53+
## Example
54+
55+
The following [!INCLUDE[tsql](../../includes/tsql-md.md)] query converts the **run_time** and **run_duration** columns into a more user friendly format. Execute the script in [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)].
5556

5657
```sql
57-
SET NOCOUNT ON;
58+
SET NOCOUNT ON;
5859

59-
SELECT sj.name,
60-
sh.run_date,
61-
sh.step_name,
62-
STUFF(STUFF(RIGHT(REPLICATE('0', 6) + CAST(sh.run_time as varchar(6)), 6), 3, 0, ':'), 6, 0, ':') 'run_time',
63-
CASE WHEN sh.run_duration > 235959 THEN
64-
CAST((CAST(LEFT(CAST(sh.run_duration AS VARCHAR), LEN(CAST(sh.run_duration AS VARCHAR)) - 4) AS INT) / 24) AS VARCHAR)
65-
+ '.' + RIGHT('00' + CAST(CAST(LEFT(CAST(sh.run_duration AS VARCHAR), LEN(CAST(sh.run_duration AS VARCHAR)) - 4) AS INT) % 24 AS VARCHAR), 2)
66-
+ ':' + STUFF(CAST(RIGHT(CAST(sh.run_duration AS VARCHAR), 4) AS VARCHAR(6)), 3, 0, ':')
67-
ELSE
68-
STUFF(STUFF(RIGHT(REPLICATE('0', 6) + CAST(sh.run_duration AS VARCHAR(6)), 6), 3, 0, ':'), 6, 0, ':')
69-
END 'LastRunDuration (DD.HH:MM:SS) '
70-
FROM msdb.dbo.sysjobs sj
60+
SELECT sj.name,
61+
sh.run_date,
62+
sh.step_name,
63+
STUFF(STUFF(RIGHT(REPLICATE('0', 6) + CAST(sh.run_time as varchar(6)), 6), 3, 0, ':'), 6, 0, ':') 'run_time',
64+
CASE
65+
WHEN sh.run_duration > 235959 THEN
66+
CAST((CAST(LEFT(CAST(sh.run_duration AS VARCHAR), LEN(CAST(sh.run_duration AS VARCHAR)) - 4) AS INT) / 24) AS VARCHAR)
67+
+ '.' + RIGHT('00' + CAST(CAST(LEFT(CAST(sh.run_duration AS VARCHAR), LEN(CAST(sh.run_duration AS VARCHAR)) - 4) AS INT) % 24 AS VARCHAR), 2)
68+
+ ':' + STUFF(CAST(RIGHT(CAST(sh.run_duration AS VARCHAR), 4) AS VARCHAR(6)), 3, 0, ':')
69+
ELSE
70+
STUFF(STUFF(RIGHT(REPLICATE('0', 6) + CAST(sh.run_duration AS VARCHAR(6)), 6), 3, 0, ':'), 6, 0, ':')
71+
END AS 'LastRunDuration (d.HH:MM:SS) '
72+
FROM msdb.dbo.sysjobs sj
7173
JOIN msdb.dbo.sysjobhistory sh
72-
ON sj.job_id = sh.job_id
74+
ON sj.job_id = sh.job_id;
75+
GO
7376
```
77+
78+
## Next steps
79+
80+
Learn more about related concepts in the following articles:
81+
82+
- [dbo.sysjobactivity (Transact-SQL)](dbo-sysjobactivity-transact-sql.md)
83+
- [dbo.sysjobs (Transact-SQL)](dbo-sysjobs-transact-sql.md)
84+
- [dbo.sysjobsteps (Transact-SQL)](dbo-sysjobsteps-transact-sql.md)
85+
- [SQL Server Agent Tables (Transact-SQL)](sql-server-agent-tables-transact-sql.md)

0 commit comments

Comments
 (0)