Refactoring SQL Code

One of the things I see software developers often talking about is how they refactor code. As they touch a class, method, etc., they may take the time to refactor the code to make it cleaner, perform better, or just add some documentation. It seems that a regular part of a software developer’s job is refactoring code in the codebase.

That is unless they see a “don’t touch this, no idea how it works” comment. There are plenty of those, and often everyone leaves that code alone.

I was thinking about this when I saw this article on strategies to refactor sql code. The article seems written more for PostgreSQL, but there are items that relate to T-SQL as well. The main thrust of the article is about trying to rewrite code to DRY (don’t repeat yourself). The more changes you can make to shrink code, either to make it easier to read or avoid repeating those copy/paste items, the better off your team will be. It’s easy to think those copies aren’t a big deal, but it’s easy to update code in one place because that solves the problem you were given, and forget to fix all the copies.

I don’t know that anyone should implement all the techniques listed, but they are things to think about. Using CTEs, Views, APPLY, the WINDOW clause, and more can help improve the health of your codebase and make it easier for all the members of your team to understand how the system works.

I wonder how many of you have a refactor mentality when you touch code, or do you tend to leave things alone and add new queries/objects/etc. to your database. I wonder if the fear of breaking something that might be used by other code is on your mind. Or maybe you suffer from “not invented here” (NIH) and just add your own code.

If you refactor code, then what things do you look to change or improve? Any tips/tricks/guidelines you’d share with others? If you don’t refactor code, why not?

I think testing is a big part of refactoring. If you have tests, then you can be less worried about your changes breaking something. There is a great video on practical refactoring. It’s from the software engineering view, and it’s long, but it’s worth a watch if you have a few moments.

I wish more people tested their SQL code and refactored poorly written (or poorly performing) code on a regular basis.

Steve Jones

Listen to the podcast at Libsyn, Spotify, or iTunes.

Note, podcasts are only available for a limited time online.

Posted in Editorial | Tagged | 1 Comment

Advice I Like: Art

Superheroes and saints never make art. Only imperfect beings can make art because art begins in what is broken – from Excellent Advice for Living

Interesting advice for living here. What is art? For many of us this is a painting, a piece of music, a theatrical performance, maybe a garden. In those cases, often inspiration comes from something broken. A broken heart, a tragedy, maybe something else.

Even in software, where I work, we might seek to create something amazing, an artistic piece of code, because we’ve seen other things that were broken and caused problems.

However.

What about love songs, the inspiration of which might be something great. I looked for some, but in most of the lyrics, there seems to be something broken. I Will Always Love You is popular, but seems to be a relationship that isn’t amazing.  Something, one of my favorites has the idea that the the author doesn’t know. There’s some lack of confidence.

Maybe the best one I found is Just the Way You Are (Billy Joel, not Bruno Mars). Here the author doesn’t care about any sort of imperfections of problems, and doesn’t seem to imply there are. He just says I accept you for what you are. That isn’t broken.

In most other songs, the singer, or the object of their affection is broken in some way, so maybe there’s more to this advice than I thought at first.

I’ve been posting New Words on Fridays from a book I was reading, however, a friend thought they were a little depressing. They should be as they are obscure sorrows. I like them because they make me think.

To counter-balance those, I’m adding in thoughts on advice, mostly from Kevin Kelley’s book. You can read all these posts under the advice tag.

Posted in Blog | Tagged , | 2 Comments

Investing for AI

The GenAI boom is growing like crazy. From hype to disasters to successes to investment to the embedding of GenAI tech into lots of products, it seems no one gets away from AI. My wife, kids, friends, they all talk about AI and alternately give me stories of huge successes or epic failures. Even those who just scroll through reels aren’t immune as we see amazing things, but we can’t trust them because of AI. Who knows what image/video/audio was actually recorded and what was generated.

Like many of you, I think AI can be amazing. Like more of you, I think it can be a really poor partner and it produces output I can’t trust. I think one of the major challenges is learning to treat an AI like a colleague whose work quality is erratic. It’s not that I can’t work with them and use their work, but I need to test, validate, and verify the code they give me does what I need, at some acceptable quality level.

Microsoft is a company investing a lot in AI, and it’s changing the company. Some of us might not like the direction as it seems that AI is being pushed for the sake of AI and to generate profits for Microsoft. Or at least revenue as I’m not sure how much profit there will be with all the compute costs of AI. However, it’s certainly affecting every product development team.

I listened to a very interesting interview with Satya Nadella talking AI, globalization, and more, including a data center tour of their new AI site in the ATL. The data center tour with Satya and Scott Guthrie is at the beginning and it’s amazing to see. The network connections in this data center are equivalent to all of Azure a few years ago. That’s impressive, especially seeing they plan to link these new generation data center with petabit networks. For someone that grew up with 300baud modems and then 2.5Mbps Arcnet, I can’t even conceive of these speeds.

As I listened to the interview, I was skeptical of Microsoft’s efforts. The hosts were as well, as they pressed Microsoft to really give them a reason why all this AI investment makes sense. The interview is long (1:27:47), but includes some interesting statements.

Satya says that AI might be the biggest think since the industrial revolution. I could see that, and I’m not sure I disagree. AI tech, with the ability to reduce the requirements to interact with a computer for everyone, is incredible. It can dramatically reduce the UX issues we constantly see with developers building things that don’t always make sense to users. For me, I love that it can handle my misspellings, something many traditionally coded systems cannot handle.

There’s also a great quote that Satya uses from a CMU professor: AI is a guardian angel or a cognitive amplifier. I think it’s both, as AI is a tool and it’s something you can use well or not. If all you have is a hammer, everything looks like a nail; that’s a famous quote. A hammer is a great tool.

Sometimes.

Sometimes it’s not the tool, and something else is needed. AI can be a great cognitive amplifier, but if you treat all problems as nails, you will let AI create a lot of problems. However, if you use it for the appropriate task, it can really help you. The AI can also see or spot things that we can miss as humans. As the world gets more complex, we deal with more things at once, or the rate of information coming to us increases, we may (will) miss things. An AI can do a better job of catching things, just like another person might catch things you miss.

The last interesting thing is on models vs scaffolding where we look at what models mean and what scaffolding or infrastructure. The example is with Excel (which Satya wish had a database), but it’s an interesting look at how we might get value from AI in getting tasks done, and saving labor with AI technology. It’s worth the listen (or read the transcript).

I found myself seeing how this might not only benefit Microsoft, but perhaps will benefit the world as other companies embrace multiple models and facilitate the ability of more people to use AI tech. I still don’t know if the ROI and costs make sense, but we will as the AI bubble bursts and this becomes a normal part of our lives in some way.

Steve Jones

Listen to the podcast at Libsyn, Spotify, or iTunes.

Note, podcasts are only available for a limited time online.

Posted in Editorial | Tagged | 3 Comments

Keeping MS Docs Up to Date

One of the things that I like about the SQL Server docs (MS Learn Docs) is that I can fix things I find wrong. For years we had downloaded Books Online from installs, then we have BOL on a site, but those were mostly updated when a new release came.

Now we have MS Learn, and a regularly changing set of docs. If you haven’t taken advantage of these docs for SQL Server, you should. Bookmark: https://learn.microsoft.com/en-us/sql/sql-server/?view=sql-server-ver17&redirectedfrom=MSDN

I help change those. It’s part of my contribution as a Microsoft MVP, but it’s also something that I enjoy because it makes my life easier. This post will look at how I do this.

Note: You need a GitHub account.

A Recent Change

Someone posted a note about multi-column primary keys, noting that the docs said we could use up to 16 columns, but they were able to do 17. I went to this page, Primary and Foreign Key Constraints , where in the first bullet list, there was a 16. This was the week of 24 Nov.

Now, a week later as I write this, it says 32.

2025-12_0124

When I saw the 16, I decided to test things. I set up scenarios, I checked against multiple versions, and I verified that 32 was the right number.

Then I clicked this edit button on the page:

2025-12_0125

When I did that, I was sent to the GitHub repo for the docs, which is in the MicrosoftDocs org. You can see what I see below. A lot of this is their markdown template, and can be ignored.

2025-12_0126

On the right is a pencil edit button. I clicked that.Note this says I’ll get a fork of this repo. That’s what I want.

2025-12_0127

When I click the pencil that, I go to the same page, but without any rendering. Note I’m still in the MS repo, but the blue note at the top says my changes will be written to my repo in a new branch.

2025-12_0128

When I knew I had seen an error, I scrolled down in the page and found the list. Here we see my 32 highlighted. This said 16 a few weeks ago.

2025-12_0129

I can edit this, so I’ll change this to 64. Don’t worry, I can’t affect the live docs. When I do this, I’ll then click “commit changes” in the upper right.

2025-12_0130

After clicking this, I get a commit dialog. Copilot tries to guess what I’ve done and it’s a good start. I typically edit the description a bit.

2025-12_0131

Once this is done, and I click Propose changes,  I get a pull request page. In this case, notice in the top image, I see this is going from my repo, from a specific branch, to the MS repo for comparison. I’m asking them to pull my changes.

2025-12_0132

Below this, I see the file(s) changed. In this case, one change.

2025-12_0133

I typically just click “Create pull request” for my changes and then the MS automation takes over. A form appears that shows the PR created and the status (if it can be merged). Since these are quick, usually there’s not problem with a clean merge.

I get an email from the automation thanking me for the contriution, letting me know an author has been notified.

2025-12_0135

If there are comments, I’ll get emails on those. When this happens, sometimes they let me know there is something else needed or I should amend my PR. Sometimes they tell me they’re closing the PR and incorporating the change into something else. I’ve had my change get someone thinking and they might take my idea and add something else in their own internal PR.

If someone approves my PR, they’ll add a tag from their side, and the change is merged and a rebuild happens. Here’s the email I get.

2025-12_0136

Then my change is live pretty quickly.

I probably submit 5-10 a year, almost one a month. I don’t find a lot of issues, though I do sometimes take the time to add a new example that might serve me, or others. I should do more of those.

If you want to submit your own corrections, feel free. If you don’t, and want me to do it, send me a note and I’ll submit the PR.

Posted in Blog | Tagged , , | 1 Comment