Tag: T-SQL Tuesday

  • Learning from Mistakes: T-SQL Tuesday #194

    We’re a week late, once again my fault. I was still coming out of the holidays and forgot to check on my host. Luckily, Louis Davidson (who did have Feb) agreed to go early. He has a nice invite, and I am glad to answer.

    This is the monthly blog party on something SQL Server/T-SQL/etc. related. I have about half of 2026 covered, but if you would like to host, I’d love to have you. Ping me on X/LinkedIn/BlueSky.

    A Mistake

    Since we aim for T-SQL, I decided to ping something I’ve done a number of times in T-SQL, and sometimes still break. However, a little testing has helped me (mostly) keep this from getting to production.

    Always have testing in place.

    I am good at T-SQL, but not amazing.  I learn things from others all the time, and these days, take help from AIs, though I do test and double check what they do.

    One of the places I’ve struggled with is with outer joins. Usually left/right outer joins where I am trying to get a list of things from a join, but filter out some of the missing items. Here’s an example from Northwind. I want a list of customers joined to orders, but I might have a way where customers filter out those who haven’t been charged freight. There’s likely some business reason, but it escapes me now.

    If I run this query, I get lots of stuff.

    2026-01_0109

    That doesn’t seem right. If I check, I see this:

    2026-01_0111

    What’s the problem here? Well, the main issue is one I keep doing, fortunately, I catch this. If I move the Freight IS NULL to  WHERE instead of the ON, it works. You can see this below.

    2026-01_0112

    If I see too much data, which can be hard to catch in large result sets, I can ask Prompt AI.

    2026-01_0113

    I get the response I’d expect from most AIs.

    2026-01_0114

    How do I test for this? Well, the best way is to have test coverage for queries. For example, I might build a test like this:

    EXEC tsqlt.NewTestClass @ClassName = N’QueryTests’ — nvarchar(max)
    go

    CREATE OR ALTER PROCEDURE [QueryTests].[TestCustomersWithoutOrders]
    AS
    BEGIN
    — Arrange
    — Create temporary table to hold expected results
    DECLARE @Expected TABLE
    (
    CustomerID nchar(5)
    )

    — Insert the expected result – customers with no orders
    INSERT INTO @Expected
    SELECT CustomerID
    FROM dbo.Customers
    WHERE CustomerID NOT IN
    (
    SELECT DISTINCT CustomerID FROM dbo.Orders WHERE CustomerID IS NOT NULL
    )

    — Act
    — Create temporary table to hold actual results
    DECLARE @Actual TABLE
    (
    CustomerID nchar(5)
    )

    — This should be the query that’s being tested
    INSERT INTO @Actual
    SELECT DISTINCT
    Customers.CustomerID
    FROM dbo.Customers
    LEFT OUTER JOIN dbo.Orders
    ON Orders.CustomerID = Customers.CustomerID
    WHERE Orders.CustomerID IS NULL

    — Assert
    — Check that we have exactly 2 results
    DECLARE @ActualCount INT =
    (
    SELECT COUNT(*)FROM @Actual
    )

    IF @ActualCount <> 2
    BEGIN
    EXEC tSQLt.Fail ‘Expected exactly 2 customers without orders, but got ‘,
    @ActualCount;
    RETURN;
    END

    — Check that we got the expected customers
    IF EXISTS
    (
    SELECT 1
    FROM @Expected e
    WHERE NOT EXISTS
    (
    SELECT 1
    FROM @Actual a
    WHERE a.CustomerID = e.CustomerID
    )
    )
    OR EXISTS
    (
    SELECT 1
    FROM @Actual a
    WHERE NOT EXISTS
    (
    SELECT 1
    FROM @Expected e
    WHERE e.CustomerID = a.CustomerID
    )
    )
    BEGIN
    EXEC tSQLt.Fail ‘The actual set of customers without orders does not match the expected set.’;
    END
    END;

    That’s a lot of code, but I can see it works. I get two customers back, which is what I expect. Lines 53-58 have my query being tested above. If I run the test, it passes.

    2026-01_0115

    If I change those lines to put the filter in the ON clause (and remove WHERE), it fails.

    2026-01_0116

    Ideally I’d have this in a proc so I can change/tune this and compare plans, run tests easily, etc.

    This is a mistake I still make at times today, albeit rarely. Now I write some tests to look for my mistake. Maybe that’s the thing I’ve learned the most: have tests for my code.

  • A Note and a Message: T-SQL Tuesday #193

    This month Mike Walsh hosts T-SQL Tuesday. It’s been quite some time since he hosted (back at #4), but he answered my call for hosts and I appreciate that. He has a really good end of year invitation that asks us to look to the past and the future.

    I’m trying to keep T-SQL Tuesday alive, but I’ve been struggling for hosts. I need more of you to host, and more people to blog.

    Blogging is incredible for your career. Take some time to get started, and if you do nothing but respond to these monthly blog parties, you’ll look better than most other candidates for jobs.

    If you want to host, ping me on X or at sjones at sqlservercentral dot ***.

    A Message to Steve in 2015

    The world seems to be changing quite quickly in in our lives. You bought a new Pebble watch this year, database automation is becoming a bigger part of your job, and there’s lots of data analysis you can do.

    Power Bi is a tool from Microsoft that is going to grow in popularity and it’s a place you ought to invest. New places to store data, new features in SQL Server, new platforms for building analytics will appear, but Power BI is a place you should spend more time in and improve your DAX skills.

    Learning to build better visualizations, especially for your volleyball coaching (which is going to continue for some time), is an investment that will really pay off in the future.

    A Word from the Future in 2035

    Steve,

    we spent a lot of time early in our career working with networks, helping manage accounts, users, security, privileges, and more. That knowledge and experience helped us understand SQL Server and other database platforms as well from the security standpoint.

    Starting in 2026, you need to revisit some of those skills, ensure you better understand how authorization and authentication works in the context of AI and agents. AI technology is changing the world, and it’s going to be heavily used by agents, but security and ensuring there are limits placed on them is going to define who has more success with AI and who has more trouble.

    Take the time to ensure that as you engage with MCP servers, task agents with work, and evolve the way you use AI that you include governance in your efforts to protect yourself from runaway agents that might go beyond what you expect, or spend more money than you want them to spend.

    Enjoy the next decade, it’s going to be amazing.

  • The Roundup for T-SQL Tuesday #192

    Last week I asked you to write about SQL Server 2025 and what things you might be looking forward to in the new version.

    First, as usual, is Rob Farley. He has an advantage, being in Australia, so his day starts earlier than most. His post this month is on string matching, and he is sad that Master Data Services is leaving in 2025. I’m not sure many people used it, but I also don’t know it was worth removing. However, there are new functions for fuzzy string matching, which are some options for helping with data quality.

    Chad write about something I’ve been asking Microsoft about for years. We are finally getting a Standard Edition version of Developer Edition. Chad writes in more detail about this and how to install it.

    Deborah writes about two features: optimized locking and ADR. Performance is always on the mind of most people who work with databases, and these two features should help improve performance.

    Andy has written on a few AI things in SQL Server 2025, so I expected more. However, I got an AG enhancement I wasn’t aware of that is coming.

    Rob in New Zealand writes about security changes in SQL Server 2025, but also notes that the Copilot Integration in SSMS 22 is really what he is looking forward to working with.

    My own post was on the backup changes, letting me backup on all replicas.

    Louis writes about regex. I was expecting more people to choose this, but glad Louis did.

    Glad to see a few people participating. If I missed you, please send me a note with the link.

  • T-SQL Tuesday #192: SQL Server 2025 Backup Changes


    I hosted this month, but I decided to put my own entry in as well. There are more things in this release than I expected, probably because of the overwhelming focus on Fabric from Microsoft that has drowned out other changes.

    I’ve been a DBA (or Ops manager) for quite a bit of my career, and one of the things I’ve thought was top priority was backups. I always want to be sure I have a recovery strategy, because if I don’t, nothing matters. Things go wrong and systems fail and I need to protect systems.

    Security is a close second, but backups really matter.

    I was excited to see that full and differential backups can be set on secondary replicas in AGs. To be fair, I had assumed this would be working by SQL Server 2022, but I hadn’t checked. Glad it’s been added. I know, I know, I questioned if you need HA, but that’s more a staffing question than one of whether HA makes sense in general.

    Backups, especially on larger systems, can be a load on the server. We’ve seen this with compressed backups natively, or with something like SQL Backup. As a result, offloading this to another system is something I’d want to do.

    Of course, there’s still the challenge of making sure you get these all organized in one place (don’t have the 1 copy of your diff only on one secondary), but that’s a separate post.