Author: way0utwest

  • Prompt EAP AI Experiments: Creating a History Table

    SQL Prompt has an EAP using an AI model to help write code. I’ve been lightly experimenting with it, since I think AI is an interesting tech and it’s going to change our jobs. Maybe for the better, maybe for the worse, but I want to know if this will help me get work done sooner.

    This post looks at one experiment.

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

    Building a New Table

    I’ve written a lot of SQL code in my career. I know how to structure things and I can often whip something up quickly in response to requests from users or customers. Recently I was trying to mock up a change for a scenario and I decided to use the SQL Prompt AI beta.

    In this case, I wanted a create a history table, similar to a temporal table. I started writing some code, but decided to use the AI to see if things were easier. I tried two experiments here, one using temporal tables, one using just normal T-SQL. Here’s what happened.

    Create a Temporal Table

    As I started documenting this, I decided to ask for a temporal table. You can see my basic code I sketched, but stopped before I created the table. Then I asked SQL Prompt to create the temporal able.

    2024-01-02 14_30_35-SQLQuery2.sql - ARISTOTLE.dlm_1_dev (ARISTOTLE_Steve (64))_ - Microsoft SQL Serv

    Here’s what I got:

    2024-01-02 14_32_42-SQLQuery2.sql - ARISTOTLE.dlm_1_dev (ARISTOTLE_Steve (64))_ - Microsoft SQL Serv

    The actual code is:

    /* Assuming the ArticlePayment table schema is unaltered before the creation of the history table */
    

    /* Step 1: Add period columns to the original table */
    ALTER TABLE dbo.ArticlePayment
    ADD
       StartDate datetime2(2) GENERATED ALWAYS AS ROW START
    , EndDate datetime2(2) GENERATED ALWAYS AS ROW END
    , PERIOD FOR SYSTEM_TIME(StartDate, EndDate);

    /* Step 2: Change the original table to be a system-versioned temporal table. This will create a new history table with a similar structure as the original table. */
    ALTER TABLE dbo.ArticlePayment SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE=dbo.ArticlePayment_History));

    That’s pretty good. I don’t usually remember what to do and often end up scanning an article like this one. An AI that provides this code might be a nice time saver, especially if I needed to do a few tables.

    Building an Audit Copy

    The way I’ve often tracked history in the past is essentially copying a table and adding some date columns.

    2024-01-02 14_37_03-SQLQuery2.sql - ARISTOTLE.dlm_1_dev (ARISTOTLE_Steve (64))_ - Microsoft SQL Serv

    This is helpful and quick. It gets the datatypes and names, and replicates what’s in the original dbo.ArticlePayment table.

    2024-01-02 14_37_37-SQLQuery2.sql - ARISTOTLE.dlm_1_dev (ARISTOTLE_Steve (64))_ - Microsoft SQL Serv

    I want to add nullability, so I asked above. You can see the results below.

    2024-01-02 14_37_51-SQLQuery2.sql - ARISTOTLE.dlm_1_dev (ARISTOTLE_Steve (64))_ - Microsoft SQL Serv

    One more prompt:

    2024-01-02 14_40_20-SQLQuery2.sql - ARISTOTLE.dlm_1_dev (ARISTOTLE_Steve (64))_ - Microsoft SQL Serv

    The result includes a new column with a default, which I like.

    2024-01-02 14_40_43-SQLQuery2.sql - ARISTOTLE.dlm_1_dev (ARISTOTLE_Steve (64))_ - Microsoft SQL Serv

    I can click the check in the upper left of the Prompt dialog to get this code accepted in my query window. From there, I can run this and then check the code into my VCS with Flyway Winking smile

    Comparison with ADS

    In many cases, I struggle to use an AI well. The public ones don’t see my schema, so if I were to prompt this in Copilot or some equivalent, I’d have to include my schema. If I don’t, I get something like this:

    2024-01-02 14_43_10-● SQLQuery_1 - SQL 2019 sandbox - Azure Data Studio

    If I include the schema (annoying, pasted from SSMS scripting and then commented out and edited, I get something that works, but I get a line at a time and have to accept each line.

    2024-01-02 14_45_10-● SQLQuery_1 - SQL 2019 sandbox - Azure Data Studio

    Thoughts on the Prompt EAP AI

    The EAP AI version of SQL Prompt is reading my schema, as SQL Prompt does, and then running my prompt through an AI LLM with the schema to decide what I mean. In this case, it knows temporal tables are system versioned ones. It produces good code there. Really, this has done the search engine lookup of how to convert a table to temporal, copied the code, and then edited it for my situation.

    If I ignore the temporal aspect and ask for a history table, it creates what I’d do, which is a copy of the table. If I need this altered, it does a good job if doing the edits. Using a little English here, instead of manually editing, actually worked pretty efficiently for me. I’d know what prompts to use now, and I can go back in my prompts copy them, and alter them for other tables.

    Even better, I asked: “create history tables for all article tables”

    I got the code below, which is pretty cool. This is a subset of related tables to articles, not all the tables in my database. Comments from Prompt at the top and then the code.

    /*  

                These scripts will create new tables named 'ArticlePaymentHistory', 'ArticlesHistory', 'ArticleAuthorHistory', 'RatingsHistory', 'UserRatingHistory' which are copies of 'ArticlePayment', 'Articles', 'ArticleAuthor', 'Ratings', 'UserRating'.

                It assume that you want exactly the same structure as the respective tables including all the columns and their types.

                The difference is that all columns in these history tables will NOT BE NULLABLE with an additional column 'ModifiedDate' that defaults to the current date and time.

                It does not copy the data from the original tables. If you need that too then make sure first none of your records from these tables have null

                records for any of the columns because now we have made all columns as 'NOT NULL' in the history tables.

            */

    CREATE TABLE dbo.ArticlePaymentHistory
    ( ArticlePaymentKey  int       NOT NULL
    , ArticleID          int       NOT NULL
    , ContactID          int       NOT NULL
    , PublishDate        date      NOT NULL
    , PaymentDate        date      NOT NULL
    , AuthorPaid         bit       NOT NULL
    , ArticlePaymentRate numeric   NOT NULL
    , ModifiedDate       datetime2 NOT NULL DEFAULT GETDATE ());
    CREATE TABLE dbo.ArticlesHistory
    ( ArticlesID          int       NOT NULL
    , AuthorID            int       NOT NULL
    , Title               char      NOT NULL
    , Description         varchar   NOT NULL
    , Article             varchar   NOT NULL
    , PublishDate         datetime  NOT NULL
    , ModifiedDate        datetime  NOT NULL
    , URL                 char      NOT NULL
    , Comments            int       NOT NULL
    , ReadingTimeEstimate time
    , CreatedDate         datetime2 NOT NULL
    , ModifiedBy          nvarchar  NOT NULL
    , HistoryModifiedDate datetime2 NOT NULL DEFAULT GETDATE ());
    CREATE TABLE dbo.ArticleAuthorHistory
    ( ArticleAuthorKey int       NOT NULL
    , ArticleID        int       NOT NULL
    , ContactID        int       NOT NULL
    , AuthorOrder      tinyint   NOT NULL
    , ModifiedDate     datetime2 NOT NULL DEFAULT GETDATE ());
    CREATE TABLE dbo.RatingsHistory
    ( RatingKey    int       NOT NULL
    , ArticlesID   int       NOT NULL
    , RatingDate   datetime2
    , Rating       tinyint   NOT NULL
    , UserKey      int       NOT NULL
    , ModifiedDate datetime2 NOT NULL DEFAULT GETDATE ());
    CREATE TABLE dbo.UserRatingHistory
    ( UserRatingKey int       NOT NULL
    , UserID        int       NOT NULL
    , ArticleID     int       NOT NULL
    , Rating        numeric   NOT NULL
    , ModifiedDate  datetime2 NOT NULL DEFAULT GETDATE ());

  • Projects To Showcase Your Skills

    It seems I’ve heard from a lot of people looking for jobs lately. I had a friend reach out and when I posted a note on LinkedIn I had someone looking for a job, I heard from a few dozen others that they were in the same situation. The last year has seen a lot of turnover in IT. There have been lots of big (and small) companies that have let staff go, for a variety of reasons. The why doesn’t matter to you if you’ve lost employment. You just need a new job.

    At the same time, I hear from lots of customers and friends that they are struggling to find good talent. They have openings, but none of the people interviewing have good skills.

    That’s an interesting mix of situations. There are jobs and companies want to hire people, and there are people needing jobs. However, there don’t seem to be enough good matches, and my guess is we have some skills mismatches here. In other words, candidates aren’t showing they have the skills employers need, or at least not at the depth employers need.

    A lot of success I’ve had in the past is being good at some tasks with a database, but also showing employers I can learn about other tasks easily. Some of that is from blogs or answers on forums, and some of that is showcasing those skills in interviews. I think that many managers will accept they might need to teach you or train you on certain things, but they want confidence you can learn and grow.

    Good interview soft skills help, but showcasing learning and growth helps as well. Blogging is a good way to show that, but what do you blog about? My advice to students is to explain how they solve the problems they’re given in coursework. Show what you learned, from where, and how you chose to solve a problem with the resources used.

    I would say that experienced workers can do the same thing. Take on some sample projects that build or design a database. Work on a sample dataset and produce queries to answer questions, explaining what the goal is and then how you solve the problem. Show how you can improve a project by refactoring code, and then proving things are better with execution plans and performance numbers.

    Show you have skills to get some things done, and the skills to learn how to solve complex problems. Hiring managers need to have this confidence in you.

    If you have ideas for a project, let us know in a comment. What types of projects have you worked on that you were proud of the solution? What sample datasets have you used to learn? We all can improve our skills in some way, and perhaps a few specific ideas will get some of you moving in the new year.

    Steve Jones

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

  • A New Word: agnosthesia

    agnosthesia – n. the state of now knowing how you really feel about something, which forces you to sift through clues hidden in your own behavior, as if you were some other person – noticing a twist of acid in your voice, an obscene amount of effort you put into something trifling, or an inexplicable weight on your shoulders that makes it difficult to get out of bed..

    There are a lot of times I’m not sure how I really feel about something. Whether it’s something in the state of the world (politics, government, culture), or it’s a technical item. There are many cases where I’m just not sure. I have to stop and think outside myself, and often think about how I would react if things were structured one way vs. another.

    In the tech world, I’ve often been a little uncomfortable with EULAs and software licensing, but I also don’t quite know if I would abandon most copyright/IP/patent/ etc. I’m not sure if I think the world (or my life/career) would be better or worse.

    Sometimes it’s smaller, like the way someone else tries to teach me a topic or present an idea, or perhaps the way that an event is organized. I don’t pretend that I know better, and try to remember that I might go about the item/task/etc. differently, but I don’t know if I would. It’s easy to react, and much harder to make decisions and act without the benefit of hindsight.

    I do use agnothesia, trying to step outside of my own emotions and feelings and look at what a situation or idea changes in me, as if I’m studying myself. I don’t often come to any resolution or learn anything, but it helps me to remember I’m just one person, part of a larger community and society, and life can be hard when you don’t have all the answers.

    Or as I feel, many at all.

    From the Dictionary of Obscure Sorrows

  • Skipping SQL Bits 2024

    SQL Bits has been my favorite data platform event for years. Both it and the PASS Data Community Summit hold special places in my heard and I enjoy going, but the community, casual feel of SQL Bits is just a bit more fun for me.

    I’m sad to be skipping SQL Bits 2024, but the timing doesn’t work for me. I saw Brent was also skipping (for different reasons), and decided to state my own reasons, since I have often attended. This was also Brent’s most commented on post in 2023, so I’m hoping to start the year off right on this blog Winking smile

    Managing Workload

    In 2023, I had 32 trips. By mid Oct, I was a bit worn out and struggling. I just wanted trips to be over with and get back home. Between my last pre-Summit event in early Nov and the PASS Data Community Summit, I was home for 12 days in a row. I hadn’t done that since May 2023.

    As a result, I’m trying to be more careful with travel workload in 2024. My boss and I meet every week or two and we review the schedule for the next couple months to manage things.

    I also coach volleyball in Q1 and I try not to miss events or practices so I try to limit my travel.

    Scheduling

    While I local SQL Bits, I’ve missed a number over the years because of scheduling conflicts. Their March events have conflicted with other events in my life, often outside of work. I preferred when they had an Aug/Sept event as well.

    This March, I am committed in March from the 9-11th, and then have to be in Atlanta on the 13th-14th. I have the next week at home, and I could potentially go to the UK on the 18th, but I have to be back on the 24th to coach an event in Denver. That means leaving Sat from the UK at the latest, and leaving SQL Bits on Friday. I’ve done that before, but I have to miss some practices in Denver that week and it’s not worth it just to indulge myself in another event.

    I also have to be back in the UK in April and travel the in between week in the US, so I’m thinking that this is a time when I could make the schedule work, but it would impact my managing my workload.

    Skipping a Year

    I’ve been blessed and honored to speak at many events. While I would like to go back and enjoy SQL Bits, I also want to make room for others and let them enjoy the experience. Missing a year is OK, and I’ll look forward to 2025 (and cross my fingers that the date doesn’t have workload or scheduling impact).

    If you’re anywhere near London or can get there in March, attend the event yourself. It’s fantastic and a lot of fun.