In short: It seems I only get PRINT messages to my SQL Server job history. What is the definition of valid history output?
Background: Just executing a statement like UPDATE product SET ...
will leave no trace in the job history (no matter whether "Include step output in history" is checked or not). Running the same statement in Query Analyzer shows
"(X row(s) affected)".
By creating a variable and assigning it the @@ROWCOUNT contents after the update I can build and PRINT a message that will include the number of affected rows. This will be included in the job history.
This works fine, but I'd like to know the definition of output (that is written to history) from a Transact-SQL Script job Step. What is included except PRINTed strings?
One might easily think that the history would show exactly the same as is shown in the Messages pane of the Query window (formerly known as the Query Analyzer) of SQL Server Management Studio – but that is not the case.
Best Answer
For T-SQL Job Steps, the "output" refers to "messages" -- notices sent via
PRINT
andRAISERROR
. Result sets are also included as "output", but only if there are noPRINT
/RAISERROR
messages, else it is only thePRINT
/RAISERROR
messages that are included.Try this test:
Job Step 1
Definition:
Output in job history:
Job Step 2
Definition:
Output in job history:
Job Step 3
Definition:
Output in job history:
Regarding the following statement in the Question:
One certainly might think that, but what is shown in the Messages tab of SSMS is not necessarily direct output from SQL Server. The "X rows(s) affected" is being generated by SSMS based on info it received from SQL Server that was not direct output but additional info that comes back in the Tabular Data Stream (TDS). The same applies to batch iteration using "GO x" where the
x
is an integer telling SSMS how many times to submit that particular batch (the one ending with the "GO" batch separator). The output in the "Messages" tab would showBeginning execution loop
and at the endBatch execution completed 4 times.
, but those are messages from SSMS and not from SQL Server.