Tag: syndicated

  • T-SQL Tuesday #168–Roundup

    Last week was the 168th T-SQL Tuesday, which I hosted. The invitation is here.

    I didn’t get much of a chance to check out the posts as I was at the PASS Data Community Summit, but I came home and started to work through them.

    This was the 8th one I’ve hosted, which makes sense as I’ve taken over managing the party from Adam Machanic and there have been a few places I’ve had to fill in for missing hosts. In any case, here’s the roundup. I’m going in order of the comments as I see them on the blog.

    Rod writes about using LAG to rewrite older cursor code that summarizes data by period. for a 30x reduction in runtime. Plus, one less cursor in the world.

    Aaron Bertrand has probably done most, if not all, of the T-SQL Tuesday parties. He’s been an expert in many aspects of T-SQL and I always look forward to reading his posts. In this one, he writes about a few different problems he’s solved with different window functions on the Stack Overflow database.

    Deb the DBA was in Seattle with me last week, but she found time to give us a way she gets visibility into long running processes from an audit table. In this case she wraps a few window functions inside of a set of MAX() queries of the data.

    Andy Brownsword a few relative queries that perform better with window functions. A good set of examples you might use in your work.

    Hugo warned me he was going to write a long post. He did. Worth a read as he delves into the execution plans behind window functions.

    My own post was on a change in SQL Server 2022 that makes it easy to re-use window definitions and not have to copy/paste them.

    I believe Rob Farley has done every T-SQL Tuesday, and this month is no exception. In this case, he shows how to look at temporal table data.

    Chad Callihan looks at Stack Overflow and how Lead() can find gaps.

    And from Twitter, I caught Barney Lawrence’s post on using FIRST_VALUE, LAST_VALUE and NULLs. I don’t often see too many people looking at first_value() and last_value(), so I liked this one.

    That’s it. If you want to host in 2024, I’ve still got some spots near the end of the year. Ping me and participate in a few of the other parties on your blog.

  • Changing the Data Type of a Primary Key–#SQLNewBlogger

    A client asked this question recently: How do I change my numeric PK to a character type?

    I decided to write a short blog on how to do this. This is the happy path, and not intended to cover all situations. I’ll write about some exceptions in a separate post.

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

    The Scenario

    A customer had a table where the PK was a number and wanted to change this to a character field. Here’s an example table with some data.

    CREATE TABLE Invoice
    ( InvoiceID   INT NOT NULL CONSTRAINT InvoicePK PRIMARY KEY
    , InvoiceDate DATE
    , CustomerID  INT);
    GO
    INSERT dbo.Invoice
       (InvoiceID, InvoiceDate, CustomerID)
    VALUES
       (1, '20230102', 3)
    , (2, '20230103', 3)
    , (3, '20230105', 4)
    , (4, '20230106', 8)
    , (5, '20230108', 11)
    , (6, '20230109', 37);
    GO
    
    

    Now, the situation was really that the customer was generating numbers for documents, but they realized their business had changed and they needed to add characters to the data.

    The Problem

    There are two things to think about here. First, what happens with the data? In this case, converting an integer to a character is easy and works. As long as the character field is long enough, this works fine. We first want to be aware of the data loss potential, though SQL Server won’t allow this.

    Second, we can’t change the data type because the PK is a constraint. If we try to change this type, we get an error:

    2023-10-23 11_01_39-SQLQuery1.sql - ARISTOTLE.sandbox (ARISTOTLE_Steve (69))_ - Microsoft SQL Server

    We really need to remove this constraint.

    If we have an outage window, this isn’t hard. If we don’t, then we have to be careful. In this case, I’ll assume we can pause the system and can make the changes without data changing in the table.

    The Solution

    The process to change the type is a few steps. I’ve shown them here.

    1. remove the PK constraint
    2. change the column
    3. add the PK constraint back

    This code will do this. I run three statements to make the change, wrapped in a transaction, with error handling to rollback if one fails

    BEGIN TRAN
    DECLARE @e INT = 0
    ALTER TABLE dbo.Invoice DROP CONSTRAINT InvoicePK
    IF @@ERROR<> 0 
      SELECT @e = 1
    ALTER TABLE dbo.Invoice ALTER COLUMN InvoiceID VARCHAR(20) NOT NULL
    IF @@ERROR<> 0 
      SELECT @e = 1
    ALTER TABLE dbo.Invoice ADD CONSTRAINT InvoicePK PRIMARY KEY (InvoiceID)
    IF @@ERROR<> 0 
      SELECT @e = 1
    IF @e = 0
         COMMIT
    ELSE 
         ROLLBACK

    This will change the data type and then reset the PK, as you can see below. With all my data intact.

    2023-10-23 11_10_38-SQLQuery1.sql - ARISTOTLE.sandbox (ARISTOTLE_Steve (69))_ - Microsoft SQL Server

    This is a simple scenario, and there are more considerations, but those are for another post.

    SQL New Blogger

    This post took me about 15 minutes to write. I took something I’d mocked up as a test for a client and then added that to this post. The code was barely changed, and I really renamed something and removed a few columns. Adding the text around this took most of the time.

    This is something that all of you could do to show that you have this skill. Changing a PK isn’t something you want to do, and it is unusual, but it does happen at times. I’ve had this happen before, and there are various other exceptions. Note I’ve added a note at the bottom to link this in to a series looking at other changes. What if the data isn’t compatible? what if the type is too short? What decisions would you make about the new PK, as int to bigint is easy, but what about char to date and possible collisions? What about identity values?

    You can do this and easily build 3-4 posts on this topic. Showcase your knowledge and you might create (and control) a fun discussion in an interview.

  • A New Word: Vaucasy

    vaucasy – n. the feat that you’re little more than a product of your circumstances, that for all the thought you put into shaping your believes and behaviors and relationships, you’re essentially a dog being trained by whatever stimuli you happen to encounter, reflexively drawn to whoever gives you reliable hits of pleasure, skeptical of ideas that make you feel powerless.

    I think a lot about how my life has gone, and how I react to things. My thoughts, my behaviors, and how my past shapes me. I do try to learn more, and I think I do a good job.

    I’m also proud of watching my kids grow and change as adults. When I look at them, I think they have a really interesting mix of things they inherited from my wife or I, and also from their experiences. They are all very different, but similar.

    Vaucasy is certainly something that I think affects all of us. There is a very human need to react to simuli, looking for pleasure. I also think we can change that and put our thoughts into shaping behaviors and thoughts, but it takes effort.

    It’s worth it.

    From the Dictionary of Obscure Sorrows

  • Deploying Indexes with SQL Compare

    I suspect many people assume this is the case, but a customer recently asked if SQL Compare handles indexes. It does, and this post shows the basics of index comparisons with no filters.

    This is a part of a series of posts on SQL Compare on my blog. You can read other posts I’ve written by clicking the link.

    The Setup

    I have two databases that are completely synced from a schema perspective.

    2023-10-30 13_56_53-SQL Compare - E__Documents_SQL Compare_SharedProjects_(local)_SQL2017.SimpleTalk

    I’ll now create an index in Compare1. This is a simple index on a single table. I use this code:

    CREATE INDEX IDX_mychar ON dbo.MyTable (Mychar)

    Once I refresh the compare, I see this. Note that I’ve selected the table that has a difference and it shows the new index. This bottom left shows the scripted version of the code I ran above.

    2023-10-30 13_58_13-SQL Compare - E__Documents_SQL Compare_SharedProjects_(local)_SQL2017.SimpleTalk

    If I create the deployment script, I see the index in it, as shown here:

    2023-10-30 13_59_03-Deployment

    By default, SQL Compare includes almost all objects and that includes indexes. There are options to change the behavior with indexes, and I’ll cover those in future posts. You can also set a filter that might exclude indexes (or include those), but those are also future posts.

    SQL Compare is a fantastic product for simplifying work and it does so much more than this. Give it a try if you own it or download an evaluation today.