SQL Server Message Printer
Print messages in SSMS instantly using a tried and trusted trick. Includes a date time stamp and option row numbers
A function we regularly employ to keep track of timings within long running Stored Procedures. This prints without waiting for a batch like PRINT. Three input parameters are used:
- @MessageDate - Pass in NULL to print the current date time as the function is called,
- @MessageText - The message text you want to display
- @MessageRows - Optionally add the number of rows affected.
SQL
Use UtilitiesGOCREATE PROC dbo.Printer(@MessageDate DATETIME,@MessageText NVARCHAR(250),@MessageRows NVARCHAR(50)) AS BEGIN IF @MessageDate IS NULL SET @MessageDate=GETDATE(); DECLARE @MessageDateString NVARCHAR(MAX)=CONVERT(NVARCHAR(20),@MessageDate,120) IF @MessageRows IS NOT NULL BEGIN RAISERROR ('%s - %s - Rows(%s)',0,0,@MessageDateString,@MessageText,@MessageRows) WITH NOWAIT END IF @MessageRows IS NULL BEGIN RAISERROR ('%s - %s',0,0,@MessageDateString,@MessageText,@MessageRows) WITH NOWAIT ENDENDGOEXEC dbo.Printer NULL,'Started',NULLWAITFOR DELAY '00:00:03'EXEC dbo.Printer NULL,'Step 1',1WAITFOR DELAY '00:00:01'EXEC dbo.Printer NULL,'Step 2',2WAITFOR DELAY '00:00:01'EXEC dbo.Printer NULL,'Step 3',3WAITFOR DELAY '00:00:01'EXEC dbo.Printer NULL,'Step 4',4WAITFOR DELAY '00:00:01'EXEC dbo.Printer NULL,'Step 5',5WAITFOR DELAY '00:00:01'EXEC dbo.Printer NULL,'Finished',NULL