A personal repository of technical notes. - CSC

How to Force SQL Server Job Step to Fail

Problem

Need to force an operating system job type to fail.

Solution

In a .NET application, set the ExitCode of the Environment object to a value other than zero. C#.NET example:

private static void TestSqlJobOutput()
{
       System.Console.WriteLine("Sample Error Message 1"); // Shows up in SQL Server job history log.
       Environment.ExitCode = 1; // Causes SQL Server job step to fail when application ends.
}

How to Write Output to SQL Server Job History Log

Problem

Need to generate custom output in job history of an operating system job type.

Solution

1) Check this box in SQL Server Management Studio: Job Step Properties / Advanced / Include step output in history

2) Generate output in job's executable. C#.NET example:

private static void TestSqlJobOutput()
{
       System.Console.WriteLine("Sample Error Message 1"); // Shows up in SQL Server job history log.
       Environment.ExitCode = 1; // Causes SQL Server job step to fail when application ends.
}