Tag: syndicated

  • Why Use TRY_PARSE(): #SQLNewBlogger

    Someone asked why I would use TRY_PARSE after I posted a question at SQL Server Central: Getting the Average. Isn’t is slower?

    A fair question. This quick post looks at why.

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    A Quick Setup

    The question above has the setup code, but what if I add another row? For example, I’ll run this .

    insert dbo.commission
    (
        salesperson
      , commission
    )
    values
    (‘Steve’, ‘A’)

    Now, let’s look at the data and run the query from the question.

    2026-07_0371

    This works. However, let’s remove the (slow) TRY_PARSE() from the aggregate.

    2026-07_0372

    Error. Why? I can’t convert “A” in the AVG to a number. It fails.

    You might think, I’ll never get bad data like this. But you might? A user might enter something you don’t expect. An AI might model this as a string, which is bad, but it happens. If it’s an EAV type table, or there are other data  items and you’re trying to extract the numbers from here, TRY_PARSE is helpful.

    SQLNewBlogger

    I wrote a post last week and this is a followup that really just took less than 5 minutes to setup and run. Plus I responded for the user in the post.

    This showcases me thinking about a question and situation and really gives an interviewer something to ask me. This lets them dive into my thought process and gives them confidence I don’t just write code without thinking.

    Add to your blog with short posts like this (or drop on LinkedIn).

  • T-SQL Tuesday #201: Temp Tables

    This month we have a new host, which I am grateful for. So many people have stopped blogging that it’s a challenge to keep this going. Jeff Taylor has an invite asking us about a core T-SQL topic that can affect the performance of your app and your server: temp tables.

    This is an interesting one for me, as I’ve changed my mind on these over the years, especially as Microsoft has made improvements to the SQL Server storage engine and query processor that have helped tempdb to perform better.

    They’ve also added things that can impact and load tempdb.

    If you want to host a T-SQL Tuesday, ping me. I’m always looking for new (or returning) hosts.

    My Answer

    My answer to Jeff’s question of temp tables as friend or foe is yes.

    They are friends.

    They are foes.

    Most things that we struggle with in database work are tradeoffs. We have to balance the demands. It’s why we say “it depends” so often because we have to find a way to do more of one thing, while accepting less of another.

    If I use temp tables in a query, I can potentially run a query to get a smaller data set that I can query with to get the results I need. I can reduce the memory grant, which might be required if the initial query scans a lot of data.

    Suppose I have a 100mm row table. If I am doing a complex join of this data on unindexed columns with multiple other tables, perhaps I want to do something like this (delivereddate might not be indexed):

    select c.customerid, o.orderid, o.delivereddate, oho.shipperid, o.salespersonid, ,qty, oh.price
    into #limitedorders
    from orderheader oh inner join orderdetail od on oh.orderid = oh.orderid
    where customerid = 12

    This can get me a filtered list of things, which I can then join this temp table with the customer, shipper, salesperson, and other tables, which will be a smaller, quicker join.

    However, a temp table can be a problem as well. If you have a query that does something like this:

    select *
    into #orderlist
    from orderheader oh inner join orderdetails od
    on oh.orderid  = od.orderid
    where oh.orderdate > dateadd(year, –10, getdate()

    And then you have some sort of query that aggregates across the years.

    select c.customername, sum(t.qty * t.price)
    from #orderlist t
    inner join customer c
    on t.customerid = c.customerid
    where customerid = 12

    I’ve done a lot of work in the first query to gather data, allocate space in tempdb, copy this over, use memory, etc. Then I am going a simple query to aggregate things, and filtering it. In this case, the developer likely followed a pattern of gather data, then sum it from other queries. It might have worked for them, but if I have 1mm orders a year, this will suck up resources.

    My Advice

    My advice is like Jeff’s. In general, try to work with a query to solve your problem. Use joins, beware of views, and make sure you’ve indexed well. Use CTEs to break down your problem, and don’t use temp tables.

    If you struggle to get a single query, or you have poor performance because of memory grants or other issues, then think about shrinking your dataset down using a temp table and indexed columns. Then join this to get other data that you need.

  • Don’t Fight with AI

    I was recently trying to handle a simple task with a few AI tools to see how well things worked. I realized that AI isn’t great for everything and there are times you need your judgment to stop fighting AI and use other tools.

    Tl;dr choose the shortest path and know your tools. In this case, just copy paste a script and results (see the bottom).

    This is part of a series of experiments with AI systems.

    The Scenario

    I have a table with some data. I wanted to duplicate this table DDL and DML for another system. Here’s my table:

    2026-07_0152

    Simple thing, right? Lots of possible ways to do this, but understand, this wasn’t the task. I was doing something else, with another goal.

    This task was just in my way.

    First Try – Prompt AI

    I use SQL Prompt all the time, so I thought, hey, AI, script this.

    2026-07_0135

    Well, not quite what I wanted. This works cross database, or if I make a new table name by editing the script in two places. But not ideal.

    2026-07_0136

    OK, I asked for the data, and this works. A bit. I only get 10 rows. To be fair, the original select I started with was top 10.

    2026-07_0142

    I then ask for the other data, and I go backwards. I don’t know why a model would go in this way. This reminds me of working with a junior person half listening to me.

    2026-07_0140

    Grrrr.

    Claude CoWork

    This seems like a cowork task. I’m not saying this is the best thing, and since I didn’t have a repo, I decided this over code. In any case, I asked for a task. Quickly Claude gave me options for 1) PoSh, 2)T-SQL, 3) something else. I picked 2 and it took about 4 minutes or so, but I got this script.

    2026-07_0146

    I had to open in VSCode, connect to SQL, and then it didn’t work:

    2026-07_0147

    Paste back into Claude, get a quick fix, maybe 15s.

    2026-07_0148

    Copy/paste the script, which runs. Certainly I could have put this back in SSMS, but I’m not sure that’s easier/harder.

    2026-07_0149

    I copy the results, which is fairly easy here.

    2026-07_0150

    I have the script I need and can move on:

    2026-07_0151

    Redgate Assistant

    We’ve added a new Redgate Assistant panel to SQL Prompt. I tried this next, and got a few results. The DDL was first, which I could copy/paste into my new query window.

    The second was a script I pasted in and ran, which gave me insert statements. Taking these results gives me about what I have above from the Claude script.

    2026-07_0145

    This was significantly faster. From prompt to result was in the 10s range and then I could get the results in a few more seconds. That’s quick, and I didn’t lose my thought context.

    The Best Way – SQL Prompt

    I’m experimenting with, and it’s been a tool I reach for often, but as I was annoyed by Claude taking so long, I realized the best way was actually this. Run the query in SQL Prompt that’s at the top. Then select all the data in the results by clicking the top left box and right click. Select “script as insert”.

    2026-07_0153

    I can then easily search/replace or edit the name of the table.

    2026-07_0154

    Doing this, once I thought about it, was about 5 seconds of effort, no context switch. Just grab this, change the name and go on with my other work on another connection.

    Use All the Tools

    I do think AI is a great tool for me. I also think it can cause me to spend more time and effort (and sometimes $$$) on simple tasks. While I’m all for experimenting, I also want to be efficient and effective.

    Fortunately, I’m somewhat paid to try different things and report on them.

    In this case, the KISS solution is best. Use Prompt what what it does best, work with your schema, code, and (lightly) data. I know I could use an MCP server, or Claude Code at al with more guidance, or something else, but those start to feel like using AI for the sake of AI and burning tokens when there are better tools.

    Not everything is better with AI. The people who succeed and prosper in this crazy AI world will embrace it when it’s most helpful and ignore it when it’s not very useful.

  • Car Update: August 2026

    It’s been some time since I provided a car update, so I decided to showcase a few things I’ve learned about my cars since the last one. In this post:

    • A Second EV: Lucid Gravity
    • Efficiency and Tires on the Tesla Model Y
    • Serendipity with a Suburban

    This is part of a series of thoughts on cars, just for fun. These are my thoughts and opinions based on my experiences.

    Our Second EV: Lucid Gravity

    We bought  a Lucid Gravity after driving it one day. We’d driven a few other EVs and then stopped by the Lucid dealer because we had an hour and it was close. We weren’t expecting to be impressed by the car, but we were. In fact, we immediately stopped thinking of the other cars.

    After a few hours of debating it, we bought one in the airport while waiting for a flight. It’s amazing to see how much easier it is to buy from Tesla and Lucid than other deals.

    It’s the nicest car I’ve ever owned, and it’s an EV with great acceleration and handling. It’s also got a squashed steering wheel, which I am surprised I like. It’s easy to control and has a nice space to rest my wrists.

    Lucid squashed steering wheel

    There are some weird things with software, and I have already learned how to “reset” the car. It took me a few minutes to get it right, which is weird. I have to press the brake and then press two buttons on the steering wheel. This didn’t fix the issue I was having with switching profiles, but it was an interesting experience to reboot the car.

    One of the things I love is the cooled and massaging seats. I might get tired of this over time, but I have a bunch of different settings and there are a few I’ve enjoyed on our drives.

    If you think about a Lucid, use my referral link. We’ll both get some credit.

    Efficiency and Tires on the Tesla Model Y

    In about 4 years of driving the Tesla Model Y, covering almost 60k miles, I had a long term efficiency of around 250Wh/mi. That’s just a bit below the rating for the car. I think I did very well, driving mostly at 45mph and lower in chill mode.

    A friend sold me the spare 18” wheels from his Model 3 when he got rid of his car. I put them on mine, mostly because he had better tread than my summer tires. I didn’t think much of this until I checked the efficiency one day.

    I was in the 205Wh/mi range with the smaller tires. Now, the software doesn’t have a setting for 18” tires on the MYLR, so I left it at the 19” stock tires, so possibly there is a calculation error, but I got > 10% efficiency improvement from changing wheels and tires. Smaller, lighter tires, but still. That’s crazy.

    As we looked for new cars, I was aiming for the smallest tires I could get, both for replacement cost and efficiency.

    Serendipity with a Suburban

    In 2006, I bought a 2001 Chevy Suburban for $6500. The car had 101,000 miles on it and we were slightly nervous, but with a family of 5, we wanted a car that would carry all of us skiing and on trips. The AC died the next year, but that car has served the family driving all over CO, WY, and NM for many years. Each of my kids had driven it, one even banged into a concrete barrier in the snow.

    I felt this was the best car investment we’d made over the years. I sold it in 2026 with 250,000 miles on it. We’ve basically done regular maintenance (oil, brakes, tires) and replaced minor parts across the roughly 150,000 miles we’ve owned it. The car was old, had rust on it, and in the hot CO summers, it was a car my wife used carrying the dogs from client to client.

    2001 Suburban in 2026

    She wanted something better.

    I had been periodically looking for a good deal when I stumbled on a 2010 Suburban with 203,000 miles on it, It was a 3 owner car, no accidents, lots of records of service.

    It was listed $6500 (I paid $6200).

    I made my wife cut short work one Sat and we drove down to a dealer, getting there just before they closed. A bit of a test drive and we bought it that night. So far, about 8k miles in, it’s performed as well as the old one.

    And it has AC.

    Still feels like a good deal and a good choice. 20 years later, same price.