简体   繁体   中英

How to print job output in the log file

I'm running daily tasks of deleting historical data as a SQL server jobs. How do I print how many rows were affected in each run?

Here's a step in the job I'm interested in (replaced the actual DB name with "Database_Name_Here")

Print CAST(GETDATE() as Datetime2 (3) )

DECLARE @Deleted_Rows_1 INT;
SET @Deleted_Rows_1 = 1;
WHILE (@Deleted_Rows_1 > 0)
  BEGIN
     BEGIN TRANSACTION
 delete top (1000) FROM [Database_Name_Here].[dbo].[SESSION_LOG]
 where start_timestamp < DATEADD(MONTH, -22, GETDATE())
SET @Deleted_Rows_1 = @@ROWCOUNT;
COMMIT TRANSACTION
   CHECKPOINT
END

If I run it from SSMS, I can see this in the messages: (1020 row(s) affected) DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Just use:

 SELECT @@ROWCOUNT

at the end of your procedure/function.

After that

  • Open Job using SSMS then go into the Job Step
  • Click on Advanced
  • Tick Log to Table
  • Tick Include Step output in History

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM