Home » Using SQL Server Startup Procedures to automate sp_BlitzWho

Using SQL Server Startup Procedures to automate sp_BlitzWho

by Vlad Drumea
0 comment

In this post I cover how to use one of SQL Server’s lesser-known features, startup procedures, to automate sp_BlitzWho in order to capture ongoing session activity as soon as the instance starts up.

What are Startup Procedures

Startup Procedures are stored procedures that reside in the master database and are executed every time the instance starts up.

Their execution is triggered when all databases are recovered and the “Recovery is completed” message is logged at startup.

Members of the sysadmin fixed server role can use the sp_procoption system stored procedure to mark a stored procedure as a startup procedure.

Requirements

In order to set a stored procedure to be marked as a startup procedure, the following requirements must be met:

  • you must be a member of the sysadmin fixed server role
  • the stored procedure must be in the dbo schema of the master database
  • the stored procedure must not contain INPUT or OUTPUT parameters

Use cases

Like in this example, it can be used to set up a lightweight activity logging by running sp_BlitzWho in a WHILE loop.

But it’s not limited to that, you can use it for various other tasks, such as:

  • archive or delete old data from logging tables on instance startup
  • log the contents of sys.configurations and/or check for configuration drift
  • use Database Mail to notify the DBA team of an instance restart

Why startup procedures and not SQL Server Agent jobs?

Startup Procedures aren’t a replacement for SQL Server Agent jobs, since they lack the robust error handling and scheduling that the latter offers.

But they are perfect for startup related tasks that can be automated on instances that do not have the SQL Server Agent service enabled.

Configuring a SQL Server startup procedure

I already have a database named DBATools in which I have the sp_Blitz stored procedures, which I keep updated using the method I’ve demoed in this post.

Now I want to add an error logging table.

Next, I create the stored procedure that will act as a wrapper for sp_BlitzWho.

Since 1 is always equal to 1, sp_BlitzWho will be executed in a loop, and WAITFOR tells SQL Server to wait 10 seconds after each execution

Now, to enable automatic execution at startup for sp_AutoBlitzWho:

Testing it

After everything is in place, all I have to do is restart the instance.

Once the instance starts up, I can confirm that the procedure was executed by checking the instance log.

Screenshot from SQL Server's current log Selected Row details: Date 2023-10-24 7:32:02 PM Log SQL Server (Current - 2023-10-24 7:32:00 PM) Source spid12s Message Launched startup procedure 'sp_AutoBlitzWho'. SEO SQL Startup Procedures automate sp_BlitzWho


Note that sp_BlitzWho outputs an informational message on every execution that will also get logged.

Screenshot from SQL Server's current log Selected Row details: Date 2023-10-24 7:39:20 PM Log SQL Server (Current - 2023-10-24 7:34:00 PM) Source spid45s Message Checking for Read intent databases to exclude SEO SQL Startup Procedures automate sp_BlitzWho


But the Checking for Read Intent databases to exclude message can be suppressed by commenting out the following line in sp_BlitzWho.

Now, to check if the procedure is actually capturing any activity from my test workload:


And, to check if any execution in the loop error-ed out:

To disable startup execution of the procedure:

Note that, even if the above command is executed, the execution loop will run until the next instance restart or until the SPID executing the stored procedure is killed.

In my case, it would be SPID 45, as seen in the second screenshot from the instance’s log.

To check for startup stored procedures, you can use the following query:

Note about the session-level options used in my procedure

In my initial test run nothing was captured to the output table, and the Errors table was full of:

Hence setting CONCAT_NULL_YIELDS_NULL, ANSI_PADDING, and ANSI_WARNINGS to ON.

Conclusion

Startup Procedure is a less-talked about, but very useful feature of SQL Server.

It can come in very handy on an instance where you don’t have other ways of ensuring specific actions are taken/started at instance startup.

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.