Louis Davidson shows a neat use for temporary stored procedures:
On another connection (on another computer for that matter), I am right no doing some pretty long loads of some test data. The script is comprised of 6 queries, and they each may take 10 minutes (not completely sure, this is my first run of the scripts). And of course, I want to get some feedback on these queries to know how long they are taking.
One common way to do this is to put a
One addition I’d make to Louis’s post is to make use of the FORMATMESSAGE() functionality that SQL Server 2016 introduced. This use case is right in its wheelhouse.
SET @message = FORMATMESSAGE(N'%s%s%s%s',
@Message,
CASE
WHEN @AddTimeToMessageFlag = 1 THEN CONCAT(N' : Message Time - ', SYSDATETIME())
ELSE N''
END,
CASE
WHEN @AddSpidToMessageFlag = 1 THEN CONCAT(N' : ProcessId - ', @@spid)
ELSE N''
END,
CASE
WHEN @AddOriginalLoginToOutputFlag = 1 THEN CONCAT(N' : LoggedInUserId - ', original_login())
ELSE N''
END);
FORMATMESSAGE() provides a moderate benefit to readability versus a lengthy CONCAT(). And if you always wanted to emit all fields versus the optional setup that Louis has in place, FORMATMESSAGE() makes the result even clearer to understand.
SET @message = FORMATMESSAGE(N'%s : Message Time - %s : ProcessId - %i : LoggedInuserId - %s',
@Message,
CAST(SYSDATETIME() AS NVARCHAR(100)),
@@spid,
original_login());
Interesting. May have to write a blog on FORMATMESSAGE, and then update my code.
Keeping parts optional was a big part of my goal. I usually just want to put out a message, and the time was generally useful. I’ve added to my current version the ability to put variable-length strings of asterisks too, so you get a page break feel:
******************
message
******************
Also, thank you for the ideas and the mention!!