Words: 544
Time to read: ~ 2 minutes
Read-Clipboard
Welcome to T-SQL Tuesday, the monthly blog part where we are given a topic and asked to blog about it. This month we have Garry Bargsley, asking us to discuss end-of-year activities.
Here
My current workplace has a change freeze in place for the end of the year. Usually, I would say that this is risk-averse.
But seeing as the nature of the business (payments) means that the Thanksgiving/Black Friday/Christmas time is the busiest time of the year, I’m willing to cut them some slack.
So, what to do when we cannot deploy to production? Oh, we’ll still be busy! There are always management-approved fixes that get through, annual processes to complete, and project planning that has to be…well, planned.
But, my priority for this end-of-year is documentation.
Docs
We have a few different tools for documentation. Examples are Confluence, Google Sheets, Google Docs, etc.
But most of the time, documentation takes the form of scripts saved to source control.
These scripts are multiprocess and cross-team dependent and can quickly end up like me doing DIY. One hand trying to steady the nail, the other wielding the hammer, and the whole situation collapsing into swear words and tears.
We can’t currently have a “hit-Enter-and-leave-it” bunch of scripts because we have to stop midway for another team’s work or to check the results of what we’ve just run.
Notebooks
If we used Notebooks, this would be so much easier. I could create the code, save a snippet of the results to the notebooks, and then future executors could see what to expect.
No-books
We don’t use Notebooks.
Plain .sql files for me, it is!
To ease the documentation burden and have some semblance of tidiness, I created a PowerShell tool that splits it out “all pretty like”.
Format-TextTable
Now, with a combination of Read-Clipboard
from the ImportExcel
module, I can grab results and turn them into a text table that I can add back into the script.
Simple example: we want to save the database name, create date, and compatibility level of the databases on an instance.
… I said simple example – not good or useful example.
SELECT
database_name = name,
create_date,
compatibility_level
FROM sys.databases;

Now, let’s copy and paste the results into a comment on that script as a form of “documentation”.

Now, this time I’m going to copy those results, and run the following code in my PowerShell Core window, before pasting into the comment block.
Read-Clipboard | Format-TextTable | Set-Clipboard

Given the option, I know which one I’d choose.
Code
You can pick up the source-code here.
Feel free to check it out and add/remove from it.
Set-Clipboard
I am aware that I could spend minutes of my time to evenly align the spaces. And, yes, I know that SSMS can do find-and-replace using simple Regex.
But if I manually structure the output every time, for all different types of columns & data types, with different spacings…
I’d be better off arguing with the company to start using Notebooks.
Until then, this works for me. Hopefully, it will work for you.
And hey, maybe it will help improve your documentation in the New Year. That has a higher chance of happening than my one to improve my DIY skills.
One thought on “T-SQL Tuesday 157 – End of Year Activity”