John McCormack DBA

SQL Server Databases and Cloud

  • Personal
    • About
  • Free Training
    • SQL Server on Amazon RDS (Free Course)
    • Free practice questions to help you pass DP-900
  • Save money in Azure
    • Azure IaaS SQL Backups – Stop burning money
    • Your Azure SQL Database and Managed Instance is too big
    • Turn the cloud off at bedtime to save 70%
    • Your Azure SQL Virtual Machine might be too big
    • Save money with Azure SQL DB serverless
    • Save up to 73% with reserved instances
    • Delete unused instances to save money in Azure
  • Hire me
    • 60 minute cost optimization
    • Let me solve your SQL Server problems
    • Take a look at my Sessionize speaker’s profile

T-SQL Tuesday #143 – Short code examples

4th October 2021 By John McCormack 28 Comments

t-sql tuesday logo

T-SQL Tuesday this month is going back to basics and its all about code. I’d like to know “What are your go to handy short scripts”?

What are those little short bits of code that you can’t live without? I’m talking about little snippets that are only a few lines, that you may even have memorised. It could be T-SQL, PowerShell, Python or anything else you use day to day.

e.g. I manage a lot of SQL agent jobs. Quite often, I need to find out which job has a certain t-sql string in the command so I’ll run:

SELECT * from msdb..sysjobs sj 
JOIN msdb..sysjobsteps sjs 
on sj.job_id = sjs.job_id 
where sjs.command like 'backup log%' 

Of course, there are many other ways to find this out including DBATools commands but sometime I just revert to memory for convenience.

Another one I like is to get the estimated completion rate of a backup or restore. Now there are better scripts than this but sometimes, nothing beats getting a quick estimation back from a couple of lines of memorised t-sql.

SELECT percent_complete pc,*
FROM sys.dm_exec_requests
order by pc desc

My invitation to you for this month’s #tsql2sday is…

I would like you to share with the community what your go to script snippets are and why you find them useful. By sharing these, you will undoubtedly be helping someone who hasn’t thought of doing it that way, and hopefully you’ll pick up some handy hints as well.

  • Any language is fine, not just t-sql
  • Please share as many as you wish
  • Perhaps you never do this and always work off saved scripts or convert your snippets to stored procedures? Tell us why this works for you.

*** The Rules ***

  • Your post must be published on Tuesday, October 12th 2021 (in any time zone).
  • Include the T-SQL Tuesday Logo and make it link to this invitation post.
  • Please add a comment to this post with a link to your own so I know where to find it.
  • Please tweet about your post using the #tsql2sday hashtag.

Thanks for taking part
John

Share this:

  • Tweet
  • Email

Related

Filed Under: front-page, SQL Server Tagged With: powershell, t-sql, t-sql tuesday

About John McCormack

John McCormack is an experienced SQL DBA with extensive knowledge of the two largest public clouds: AWS and Azure.

Comments

  1. Rob Farley says

    12th October 2021 at 2:15 am

    Thanks John. Mine’s at http://blogs.lobsterpot.com.au/2021/10/12/go-to-scripts/

    Reply
  2. kchant says

    12th October 2021 at 11:41 am

    Thanks for hosting John, here’s my contribution for this month.

    https://www.kevinrchant.com/2021/10/12/t-sql-tuesday-143-two-of-my-personal-go-to-scripts/

    Reply
  3. aaronbertrand says

    12th October 2021 at 12:42 pm

    Thanks John! Here’s my twisted entry: https://sqlblog.org/2021/10/12/t-sql-tuesday-143-worst-metadata

    Reply
  4. sqljeff says

    12th October 2021 at 2:54 pm

    Thanks for hosting this month! Here’s my post: https://sqladm.in/posts/tsql-tuesday-143/
    -Jeff

    Reply
  5. Chad Baldwin says

    12th October 2021 at 3:18 pm

    Great topic! Thanks John!

    https://chadbaldwin.net/2021/10/12/tsql-tuesday-short-code.html

    Reply
  6. Kenneth Fisher says

    12th October 2021 at 3:22 pm

    Thanks for hosting John! Great topic. Here’s my contribution.
    http://sqlstudies.com/2021/10/12/code-examples-t-sql-tuesday-143/

    Reply
  7. amtwo says

    12th October 2021 at 8:39 pm

    Howdy John! Here’s my post on my favorite short scripts!
    https://am2.co/2021/10/t-sql-tuesday-143-my-favorite-short-scripts/

    Reply
  8. brimhj says

    12th October 2021 at 10:10 pm

    Thanks for hosting John. Here’s my submission: https://bit.ly/3lAOMmF

    Reply
  9. Tom says

    12th October 2021 at 11:26 pm

    Thanks for hosting
    https://straightforwardsql.com/posts/short-code-examples/

    Reply
  10. Mikey Bronowski says

    12th October 2021 at 11:45 pm

    Thanks for hosting John. Here is my post https://bronowski.it/t-sql-tuesday-143-short-code-examples/

    Reply
  11. Todd Kleinhans says

    13th October 2021 at 12:09 am

    Thanks again for hosting John! Here is my post: https://toddkleinhans.wordpress.com/2021/10/12/t-sql-tuesday-143-import-this/

    Reply
  12. diligentdba says

    13th October 2021 at 1:50 am

    https://curiousaboutdata.com/2021/10/12/tsql-tuesday-143-short-code-examples/

    Reply
  13. CallihanData says

    13th October 2021 at 3:01 am

    Thanks for hosting! My post is here https://callihandata.com/2021/10/12/t-sql-tuesday-143-handy-short-scripts/

    Reply
  14. Deepthi says

    13th October 2021 at 4:39 am

    Thanks so much John for hosting this month of T-SQL Tuesday!
    Here is my post:
    https://dbanuggets.com/2021/10/12/t-sql-tuesday-143-short-code-examples/

    Reply
  15. Koen Verbeeck #BLM 🌈 (@Ko_Ver) says

    13th October 2021 at 9:29 am

    I tried to post a comment here yesterday on my mobile device, but apparently that failed 🙂 Here’s my contribution https://sqlkover.com/t-sql-tuesday-143-short-code-examples/

    Reply
  16. way0utwest says

    13th October 2021 at 11:11 am

    Thanks for hosting. I’m late, but here’s my post: https://voiceofthedba.com/2021/10/13/t-sql-tuesday-143-short-code/

    Reply
  17. diligentdba says

    13th October 2021 at 12:49 pm

    Hi John, my contribution, for some reason it didnt appear in comments. https://curiousaboutdata.com/2021/10/12/tsql-tuesday-143-short-code-examples/

    Reply
  18. John McCormack says

    18th October 2021 at 6:09 pm

    Sorry if I took a while to moderate your comment, I had a few days away from the computer last week.

    Reply

Trackbacks

  1. T-SQL Tuesday #143 – Short code examples – T-SQL Tuesday says:
    5th October 2021 at 4:18 pm

    […] Invitation from John McCormack. […]

    Reply
  2. T-SQL Tuesday 143 - Two personal go-to scripts - Kevin Chant says:
    12th October 2021 at 10:59 am

    […] can find out more about the invite to share personal go-to scripts by clicking this link about the T-SQL Tuesday 143 invitation or on the image […]

    Reply
  3. T-SQL Tuesday #143 – Short Code Examples – Deb the DBA says:
    12th October 2021 at 2:30 pm

    […] It’s another edition of T-SQL Tuesday! This month, John McCormack (b | t) asks us: […]

    Reply
  4. T-SQL Tuesday #143 – Random Fun | Every Byte Counts says:
    12th October 2021 at 4:10 pm

    […] month’s T-SQL Tuesday is hosted by John McCormack, who asked participants to blog about Short Code Examples.A few years ago, I wrote a similar blog about T-SQL Tools that I could not live without. Those were […]

    Reply
  5. T-SQL Tuesday #143: My favorite short scripts - Andy M Mallon - AM² says:
    12th October 2021 at 8:36 pm

    […] this month’s T-SQL Tuesday, John McCormack (blog|twitter) asks us: “What are your go-to short, handy […]

    Reply
  6. T-SQL Tuesday #143: import this | Todd Kleinhans says:
    13th October 2021 at 12:06 am

    […] to his post, we can pick any snippet or consistent goto code we reach for on a regular […]

    Reply
  7. T-SQL Tuesday #143 Wrap Up - John McCormack DBA says:
    19th October 2021 at 6:04 pm

    […] T-SQL Tuesday #143 – Short code examples […]

    Reply
  8. 3 Important Queries for Testing Your Availability Group Endpoints – SQLServerCentral says:
    1st December 2021 at 9:20 am

    […] Another take on the term “short”, could be that it takes just a short amount of time to pull out a saved script to perform the task at hand (some routine task you may perform but doesn’t quite rise to the level of automation such as what Aaron Bertrand shared here). Please go and check the invite from John – here. […]

    Reply
  9. Endpoints: 3 Great Queries to Test AG Endpoints | SQL RNNR says:
    26th December 2021 at 11:18 pm

    […] Another take on the term “short”, could be that it takes just a short amount of time to pull out a saved script to perform the task at hand (some routine task you may perform but doesn’t quite rise to the level of automation such as what Aaron Bertrand shared here). Please go and check the invite from John – here. […]

    Reply
  10. T-SQL STUFF - Taking it to the limit - John McCormack DBA says:
    11th April 2023 at 5:48 pm

    […] limit, please feel free to let me know the results in the comments. I hope it was a fun read, back when I hosted T-SQL Tuesday, I asked about snippets of useful code. Although this is not so useful, it’s the type of topic […]

    Reply

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

John McCormack · Copyright © 2025

 

Loading Comments...