Using AI to Test a SQL Query

Someone sent me this code.

WITH p AS
 ( SELECT ID, ROW_NUMBER() OVER (ORDER BY ID ASC) AS RN
 FROM wp_posts
 WHERE post_parent = 94341; )
 UPDATE p SET menu_order = RN

That’s not great code. In fact, it won’t run. I know that, but does an AI? Let’s see how Copilot does.

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

The First Test

I entered this prompt, but accidentally hit Enter. When I then pasted in the code and hit enter, I got this:

2025-05_line0012

That is just about what I would have sent the author to let them know this code doesn’t work. It’s good to see a GenAI LLM can recognize this.

I then pasted in the entire prompt which was: “give me a table with the schema to match this code: WITH p AS ( SELECT ID, ROW_NUMBER() OVER (ORDER BY ID ASC) AS RN FROM wp_posts WHERE post_parent = 94341; ) UPDATE p SET menu_order = RN

The Gen AI response was polite, with this answer:

Certainly! Based on your SQL code, it looks like you need a table schema that matches the structure of wp_posts, particularly including ID, post_parent, and menu_order. Here’s a possible schema:

I then had this code, with a copy button that I could copy/paste into SSMS.

CREATE TABLE wp_posts (
     ID INT PRIMARY KEY,      -- Unique identifier for each post
     post_parent INT,         -- Parent post ID (for hierarchical structures)
     menu_order INT           -- Order value for arranging posts
);

That’s what I would have done to mock this up for testing. You might guess the next step: data.

Here’s my prompt: create some test data for the table with Ids that increment but are not necessarily sequential and post_parent values that match the ID values

That got my this response.

2025-05_line0016

That’s not bad, but it’s a limited set of data. Let’s ask for more.

2025-05_line0031

Below this the GenAI LLM gave me a summary that explains what it did.

2025-05_line0032

Next, I wanted a test. I used this prompt: write a tsqlt test to verify that the update statement works to reset the menu_order to sequential values for a particular post_parent

I got a nice response and the code I’m showing below the image.

2025-05_line0033

Here is the code:

-- Step 1: Create a test class
EXEC tSQLt.NewTestClass 'TestMenuOrderUpdate';

GO

 

— Step 2: Define the test
CREATE PROCEDURE TestMenuOrderUpdate.[test_UpdateMenuOrder_Sequential]
AS
BEGIN
— Arrange: Fake the wp_posts table
EXEC tSQLt.FakeTable ‘dbo.wp_posts’;

 

INSERT INTO wp_posts (ID, post_parent, menu_order)
VALUES
(101, 94341, 5), (102, 94341, 3), (103, 94341, 8),
(104, 94341, 1), (105, 94341, 7);

 

— Act: Run the update logic
WITH p AS (
SELECT ID, ROW_NUMBER() OVER (ORDER BY ID ASC) AS RN
FROM wp_posts
WHERE post_parent = 94341
)
UPDATE wp_posts
SET menu_order = p.RN
FROM p
WHERE wp_posts.ID = p.ID;

 

— Assert: Check if menu_order is sequential
EXEC tSQLt.AssertEqualsTable ‘ExpectedResults’, ‘wp_posts’;

 

END;
GO

 

— Step 3: Create expected results table for assertion
CREATE TABLE ExpectedResults (
ID INT PRIMARY KEY,
post_parent INT,
menu_order INT
);

 

INSERT INTO ExpectedResults (ID, post_parent, menu_order)
VALUES
(101, 94341, 1), (102, 94341, 2), (103, 94341, 3),
(104, 94341, 4), (105, 94341, 5);

 

— Run the test
EXEC tSQLt.Run ‘TestMenuOrderUpdate’;


This is where I stopped. This code runs, though it’s brittle in that the Expected Results table is a real table and not a temp inside the test. I can easily edit that and likely quicker than the GenAI can. It’s copy/pasting some code for me from one spot to another. I also need to add a CREATE OR ALTER to the proc.

Summary

A quick use of an AI that easily saved me 5-10 minutes to mock of a scenario and show someone what they were doing wrong and also testing their code. I mock up things regularly to test them, and often do something like this to answer questions. This is a good place where AI can save some time, and might be worth using. I’m also glad to see it recognize and write some decent SQL code for me, especially the insert statement for test data.

Posted in Blog | Tagged , , , | 1 Comment

Republish: More Linux than Windows

It’s Memorial Day in the US and a holiday. With a busy schedule, I didn’t realize this was a holiday and started to schedule some things before my wife reminded me.

So while I tackle some chores, you get a republish of More Linux than Windows.

Posted in Editorial | Tagged | Comments Off on Republish: More Linux than Windows

DevOps Devour Hour Slides

Here are the slides from my talk today at the Redgate NYC Devour Hour: Architecting Zero Downtime Deployments.pptx

The Repo is here: https://github.com/way0utwest/ZeroDowntime

An interesting question on feature toggles: Would you use Extended Properties for a feature toggle flag?

My answer:

I wouldn’t. I find Extended Propeties (and triggers) to be hidden inside databases and I don’t like hidden things. They fall through the cracks, it’s hard to remember they exist, and more. Plus Extended properties aren’t easily programmed.

I would love

alter table add (or update) extended property

and a way to query these

select * from table.properties

Instead we have a messed up sp_add/update property with a very, very unintuitive set of parameters.

Posted in Uncategorized | Tagged , , | Comments Off on DevOps Devour Hour Slides

Advice I Like: Take Two Trips

If you have any doubt about being able to carry a load in one trip, do yourself a huge favor and make two trips – from Excellent Advice for Living

As I get older, this tends to resonate more with me. Certainly I’ve learned not to strain my body because pushing past a limit is painful for days or weeks (or months).

However, this “carry a load” can make sense in more than just the physical carrying-a-bunch-of-stuff situation. This is a similar situation is you’re trying to accomplish too much in a day, or a sprint, or committing to too many things in a short period of time. It also applies when you try to communicate too many things at once.

I live my life in a busy fashion, usually having too much stuff on my list of things to do. I’m perpetually behind on fixing things on the ranch, taking care of the house, cooking meals, getting to the gym, improving my coaching skills, etc. I rarely take a day off from not doing something to make my (or someone else’s) life better. That’s a little of how I am built.

However, I have learned to pace myself a bit more and be cautious about how many things I tackle in a day. I have projects that I might try to rush through in a day and perhaps not get them done the way I expected. Instead, I might break them up across two days. Or two sessions.

I’ve also learned to communicate one thing and not try to get people to think about three things at once. I try to get myself, and others, to focus on the one important thing that we’re talking about at that moment.

And, of course, I try not to carry too much at once. I find myself using carts or UTVs rather than piling everything into my arms.

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 , | Comments Off on Advice I Like: Take Two Trips