In the previous example, we took a command that runs manually to alter the index fill factor to adjust it from 80 to 100%. If you run it manually, this really means reactionary, and it takes one of the most precious resources, your time. What are the manual steps of this?
- The selection of the databases
- The trigger to say I need this to run
- The time to actually execute the code
Selecting the database can be solved using one of two stored procedures sp_msforeachdb or called sp_foreachdb given out by Brent Ozar’s FirstResponderKit.
The parameters for the sp_msforeachdb are:
The paramters for sp_foreachdb are:
When you see the code implemented, you will see why you want to use sp_foreachdb over sp_msforeachdb. For me, the parameters of @exclude_list, @system_only and, @user_only sold me. But there are a lot more possibilities also. Read the documentation and the code. You will see a lot of good patterns here.
The trigger was to run the code manually. We can create an agent job to run this on a schedule. And this allows it to run without you in the office. Meaning the third item, “time,” resolved itself.
From the parameters listed above in both stored procedures, you select SQLCommand this is text passed in a SQL script with a few adjustments. You have to quote the string, so every quote also needs a quote, and you see stuff like ‘’ (double single quotes) all over the place, and the SSMS editor gets confused when color-coding it. Also, with complex code that naturally contains single quotes, carriage returns, or line feeds, it gets a tad messy. You also have a special variable, “?” this stores the database you are working in.
Here is the overview of the code:
- Create a temp table, variables, and the string of the command we are going to run
- Call this with sp_foreachdb inserting into the temp table, repeat until all the databases have been processed.
- Loop through the table, one row at a time
- With the for executing sp_executesql and other @SQLCMD with data from the table
- After the loop, clean the house. I like to do this initially to be sure.
Step 1 – Create the variables and temp table
Step 2 – create the data collector to pass to sp_MSforeachdb
If you use the improved for each database code:
I ran it on a database, with the command checking for <100% instead of <=80%. Yes, this gives me all indexes, but I am only testing, and here are the results:
Step 3 – loop through the temp table
We have to get the max iterator and an integrator counter
Prep up the first command to run. I like calling this priming the pump. I have ever gone camping you understand the seeding the pump with water to allow the pump to work. Now we are ready to enter the loop and print statement to see what would be running. Finally, we increment the iterator and loop.
Step 4 – Add the sp_executesql call before the print command.
Step 5 – remove the temp table
Now the fun part, assemble it all together into one package. Schedule it to run.
If you want, you can email the results. But you should also email on a failure. If you notice, I clean the house before and after it runs just to be sure all is good, at any time.
Things I don’t like about this the double single quotes make readability hard. You have to remember to force your white space in the command. Also, while debugging the code in a single line of text with no formatting.