Live classes start in 2d 09h 31m — Save your seat

T-SQL Code Quality and Query Patterns

Maintainable T-SQL and patterns that avoid correctness and performance problems.

106 associated posts102 primary posts

T-SQL & Development

T-SQL Tuesday Roundup: You Know A Query’s Gonna Be Bad When…

Your queries won’t tune themselves. My 2027 Live Class Season Pass is $995 (normally $4,395), with live Fundamentals and Mastering classes plus recordings. Register by October 31 to join Fundamentals Week, November 2–6. See the pass.

For this month's T-SQL Tuesday, I invited y'all to tell me how you know a query's gonna be bad when you open it.

Read more about T-SQL Tuesday Roundup: You Know A Query’s Gonna Be Bad When… 14 comments — Join the discussion
T-SQL & Development

I Know a Query’s Gonna Be Bad When… #TSQL2sday

Your queries won’t tune themselves. My 2027 Live Class Season Pass is $995 (normally $4,395), with live Fundamentals and Mastering classes plus recordings. Register by October 31 to join Fundamentals Week, November 2–6. See the pass.

I open it up and the first thing I see is a comment with a changelog showing a long list of dates and author initials.

Read more about I Know a Query’s Gonna Be Bad When… #TSQL2sday 15 comments — Join the discussion

How to Format Your Queries in SSMS

Microsoft SQL Server Management Studio 22.7 added the ability to format your T-SQL queries by right-clicking and clicking Format SQL, or by hitting control-K, control-Q on your keyboard:

You're probably not going to like the default formatting out of the box because you're the kind of person who has strong, passionate feelings and likes to argue about them on the Internet. Good news! The configurations are really flexible. Go into Tools, Options, SQL Formatter:

Read more about How to Format Your Queries in SSMS 10 comments — Join the discussion

SSMS v22.4.1: Copilot is GA. So What’s It Do Right Now?

Copilot in SSMS has two parts. Usually people focus on the pop-out Copilot chat window, and that's useful for sure, but honestly I think you're going to get way more mileage out of the code completions feature, right away, because it blends in with your existing workflows. Let's say that I'm working with the Stack…

Read more about SSMS v22.4.1: Copilot is GA. So What’s It Do Right Now? 14 comments — Join the discussion

How to Help Copilot Encourage Good Database Standards

I know a lot of y'all lag behind on upgrading SSMS, but v22.3 just introduced something that you need to be aware of. It's going to impact any of your users who DO upgrade their SSMS, or who use Github Copilot. There's something that you can do in order to improve Copilot's code quality and make it match your preferred coding standards.

Read more about How to Help Copilot Encourage Good Database Standards 17 comments — Join the discussion
T-SQL & Development

Which Should You Use: VARCHAR or NVARCHAR?

Your queries won’t tune themselves. My 2027 Live Class Season Pass is $995 (normally $4,395), with live Fundamentals and Mastering classes plus recordings. Register by October 31 to join Fundamentals Week, November 2–6. See the pass.

You're building a new table or adding a column, and you wanna know which datatype to use: VARCHAR or NVARCHAR?

Read more about Which Should You Use: VARCHAR or NVARCHAR? 29 comments — Join the discussion
T-SQL & Development

The Query Tuning Trick You Should Use More: Pagination

When I'm tuning queries, the normal answer is to make the query perform better - either via changing the T-SQL, adding hints, or adding indexes so that the data's better prepared for the query.

However, sometimes when I'm looking at the output of sp_BlitzCache, I scroll across to the Average Rows column and double-check that the query's actually returning a reasonable number of rows out in the wild.

Read more about The Query Tuning Trick You Should Use More: Pagination 6 comments — Join the discussion
T-SQL & Development

Can AI Rewrite Bad Queries in Seconds? Kinda.

When I see horrific code, stuff that would take a lot of manual labor to fix, I like to lob it over to an LLM like ChatGPT just to see how it does.

For example, on a call with a client, I opened up one of their slowest pieces of code to find that it was running a cursor, processing data row by row rather than working in sets. To simulate it, I've written up a few stored procedures against the Stack Overflow database:

Read more about Can AI Rewrite Bad Queries in Seconds? Kinda. 13 comments — Join the discussion
T-SQL & Development

I Feel Sorry for Untrained Developers Using Entity Framework.

Most of the time, I love Entity Framework, and ORMs in general. These tools make it easier for companies to ship applications. Are the apps perfect? Of course not - but they're good enough to get to market, bring in revenue to pay salaries, and move a company forwards.

However, just like any tool, if you don't know how to use it, you're gonna get hurt.

Read more about I Feel Sorry for Untrained Developers Using Entity Framework. 23 comments — Join the discussion
Performance Tuning

Find Posts with the Wrong CommentCount: Answers & Discussion

Your Query Exercise was to find denormalization accuracy problems: checking the accuracy of a reporting column, Posts.CommentCount. There were two parts: finding the top 100 most problematic Posts with the biggest variances, and thinking about a long term solution to keep the CommentCount accuracy as high as practical. Question 1: Finding the Problematic Posts Your…

Read more about Find Posts with the Wrong CommentCount: Answers & Discussion 4 comments — Join the discussion
Performance Tuning

Query Exercise: Find Posts with the Wrong CommentCount

For 2024, I'm trying something new: weekly homework challenges! For this week's query challenge, we're going to do some data validation. Pop open the Stack Overflow database and check out the CommentCount column on the Posts table: In theory, that's a fast way to check how many Comments a particular Post has, without querying the…

Read more about Query Exercise: Find Posts with the Wrong CommentCount 31 comments — Join the discussion
T-SQL & Development

FOR XML PATH Changed The Way I Think About T-SQL #TSQL2sday

The first time I saw FOR XML PATH being used to generate a comma-delimited list, I think I stared at it, shook my head to clear the cobwebs, stared at it some more, and then closed the code editor thinking it was complete witchcraft.

And that same thing probably happened the next several times, too.

Read more about FOR XML PATH Changed The Way I Think About T-SQL #TSQL2sday 4 comments — Join the discussion