Category: Uncategorized

  • Daily Copying 23 Apr 2020

    I’ve started to add a daily coping tip to the SQLServerCentral newsletter, which is helping me deal with the issues in the world. I’m adding my responses for each day here.

    Today’s tip is to take a small step towards an important goal.

    Goals are interesting. I’ve struggled with them for most of my life, and I dislike setting them and forcing myself to meet some arbitrary thing. I find life comes pretty fast, and sometimes throws me a lot of curve balls. That being said, I’ve had a lot of success and I rarely find myself lacking for things I’ve accomplished, both at work and in life.

    That being said, I’ve let a lot of things go, partially because life is busy and I get caught up with it. I don’t have any great big goals, but I do have a few things nagging me.

    Around the ranch there are always things to work on. Some of them are priorities, and often those are the ones I don’t love working on. I get satisfaction from fixing feeders, but I don’t really enjoy it. This was most of my last Sunday. Bracing and replacing old wood.

    20200419_144558_HDR

    However, I do like tackling some new things. One of the we’ve done a few times, more in this stay at home time, and want to do more, is sit outside in the evening. As we did recently with a friend, 6ft apart.

    92449806_10156994308161937_2449172634886209536_o

    I’ve been wanting to build some Adirondack chairs for this space, with some built in tables, or at least wine glass holders. These chairs are OK, but we need more, and my wife would like to encourage boarders to come and sit and watch their horses.

    I’d like to build something like this, which really could be from some of the scrap wood I have around. I’ve got lots of odd sizes that likely would work here, at least for a prototype.

    2020-04-21 10_28_22-(305) Pinterest

    I’m hoping to get slighly started with this over the weekend. It’s not a priority, but tackling something new feels good. I’m aiming to get the basic frame measured and cut. That would be a good first step.

  • Basic Sequences–#SQLNewBlogger

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

    I haven’t used sequences much in my work, but I ran into a question recently on how they work, so I decided to play with them a bit.

    Sequences are an object in SQL Server, much like  a table or function. They have a schema, and are numeric values. In fact, the default is a bigint, which I think is both good, and very interesting. Since this will implicitly cast down to an int or other value, that’s good.

    The sequence is created like this:

    CREATE SEQUENCE dbo.SingleIncrement
      AS INT
      START WITH 1
      INCREMENT BY 1;
    GO

    These can be similar to identity values, and in fact, if I make 5 calls to this object, I’ll get the numbers 1-5 returned. Here I’ve made one call.

    2018-12-04 13_19_48-SQLQuery6.sql - dkrSpectre_SQL2017.sandbox (DKRSPECTRE_way0u (55))_ - Microsoft

    This is interesting, as the NEXT VALUE FOR is what accesses the sequence and returns values. I can use this in some interesting ways. For example, if I have to insert values into a table, I can do this:

    CREATE TABLE SequenceTest
    ( SequenceTestKey INT IDENTITY(1,1)
    , SequenceValue INT
    , SomeChar VARCHAR(10)
    )
    GO
    INSERT dbo.SequenceTest
    (
         SequenceValue,
         SomeChar
    )
    VALUES
       (NEXT VALUE FOR dbo.SingleIncrement, 'AAAA')
    , (NEXT VALUE FOR dbo.SingleIncrement, 'BBBB')
    , (NEXT VALUE FOR dbo.SingleIncrement, 'CCCC')
    , (NEXT VALUE FOR dbo.SingleIncrement, 'DDDD')
    , (NEXT VALUE FOR dbo.SingleIncrement, 'EEEE')

    When I query the table, I see:

    2018-12-04 13_22_50-SQLQuery6.sql - dkrSpectre_SQL2017.sandbox (DKRSPECTRE_way0u (55))_ - Microsoft

    Notice that the sequence number is off by one from the identity. This because I first accessed the sequence above.

    The sequence is independent of a table or columns, unlike the identity. this means, I can keep the sequence numbers going between tables. For example, let’s create another table.

    CREATE TABLE dbo.NewSequenceTest
    ( NewSequenceKey INT IDENTITY(1,1)
    , SequenceValue INT
    , SomeChar VARCHAR(10)
    )
    GO

    Now, we can run some inserts to both tables and see what we get.

    INSERT dbo.NewSequenceTest VALUES (NEXT VALUE FOR dbo.SingleIncrement, 'FFFF')
    INSERT dbo.SequenceTest    VALUES  (NEXT VALUE FOR dbo.SingleIncrement, 'GGGG')
    INSERT dbo.NewSequenceTest VALUES (NEXT VALUE FOR dbo.SingleIncrement, 'HHHH')
    INSERT dbo.SequenceTest    VALUES  (NEXT VALUE FOR dbo.SingleIncrement, 'IIII')
    INSERT dbo.NewSequenceTest VALUES (NEXT VALUE FOR dbo.SingleIncrement, 'JJJJ')

    After running the inserts, I’ll look at both tables. Notice that the values for the sequence are interleaved between the tables. The first insert to the new table has the value, 7, which is the next value for the sequence after running the inserts for the first table.

    2018-12-04 13_27_14-SQLQuery6.sql - dkrSpectre_SQL2017.sandbox (DKRSPECTRE_way0u (55))_ - Microsoft

    In these tests, I’ve used 11 values so far. I can continue to use values, not just for inserts, but elsewhere.

    2018-12-04 13_34_02-SQLQuery6.sql - dkrSpectre_SQL2017.sandbox (DKRSPECTRE_way0u (55))_ - Microsoft

    This behavior is both fun, handy, and useful, but also dangerous. These values get used when I query them, whether the inserts work or not. Here’s a short test to look at this:

    ALTER TABLE dbo.SequenceTest ADD CONSTRAINT SequencePK PRIMARY KEY (SequenceTestKey)
    SELECT NEXT VALUE FOR SingleIncrement
    SET IDENTITY_INSERT dbo.SequenceTest ON
    INSERT dbo.SequenceTest VALUES (NEXT VALUE FOR SingleIncrement, 'ZZZZ')
    SET IDENTITY_INSERT dbo.SequenceTest OFF
    SELECT NEXT VALUE FOR SingleIncrement

    This gives me an error:

    2018-12-04 13_36_52-SQLQuery6.sql - dkrSpectre_SQL2017.sandbox (DKRSPECTRE_way0u (55))_ - Microsoft

    and I can see the last SELECT has the next sequence value.

    2018-12-04 13_36_45-SQLQuery6.sql - dkrSpectre_SQL2017.sandbox (DKRSPECTRE_way0u (55))_ - Microsoft

    There are a lot more to sequences, but I’ve gone on long enough here. This is a good set of basics to experiment further, which I’ll do in future posts.

    SQLNewBlogger

    This post went on longer than expected, and it was more of a 15-20 minute writeup as I set up a couple quick examples, tore them down, and rebuilt them with screenshots for the post.

    This is a place where I can show I’ve started to learn more, and by continuing with other items in this series, I’ll show some regular learning.

  • The Short Summit

    This week is the 2018 PASS Summit, the largest conference devoted to SQL Server and the Microsoft Data Platform. This is the 20th Summit, and I’m sure there are a few people that have been to all of them. I think I’ve missed 3, though I was at the first one and I’ll be there later this week for a short trip.

    The annual Summit used to be an event that I looked forward to most of the year, a time when I’d see friends from all over the world that I only saw in person once a year. I might email, tweet, etc. with them many times, but the PASS Summit was one of the few times we’d be able to shake hands and really talk with each other.

    The world has changed a bit, with many more SQL events from SQL Bits, SQL Saturdays, Data Relay, and more that take place all over the world, and at every time of the year. If you want to talk data platform with colleagues, get inspired, learn something, or just share a beverage, you have many different opportunities each year. I get to more than my share, and I see many of my friends multiple times a year. I love that, but I also miss the excitement of there being just one event.

    That isn’t going to change and we’ll continue to have multiple events. I do think the PASS Summit is still the best place in the US to get excited about the data platform, talk with Microsoft developers, and share information with your peers. There are lots of social events, and if you get the chance to attend, it’s an action packed week. You should plan on being busy, being tired, and talking to lots of people. Attend parties, for the networking if for no other reason, and engage with others.

    Unfortunately I won’t do much of that this week. The timing this week is worse for me, personally, and I’m a little worn out from other events and travel this year. I’ll arrive Thursday and leave Friday, making this the shortest trip ever for me to a PASS Summit. I know I’ll be tired, but it’s always great to see friends and meet new people. Please, don’t hesitate to say hi, shake hands, or take a picture with me if you’re there.

    Steve Jones

    The Voice of the DBA Podcast

    Listen to the MP3 Audio ( 3.3MB) podcast or subscribe to the feed at iTunes and Libsyn.

  • The Trigger Roundup–T-SQL Tuesday #106

    This month was my turn to host T-SQL Tuesday. I chose Trigger Headaches or Happiness as the topic, and I am glad that there have been quite a few responses.

    I started the review almost immediately,and here are a few highlights. I separated these based on how I first thought of them after reading the post. If you think I’ve mischaracterized one, let me know.

    In case you don’t want to add, it’s 9 to 13, so triggers are a headache.

    Helpful Triggers

    I have to start with Burt Wagner, who gets top billing while wearing a Hawaiian Shirt. Thanks, Burt, and interesting solution. In the case where Burt wants to use Temporal Tables in pre-SQL 2016 instances, he uses triggers to manage the history table. I think that’s a great way to use triggers, and while slightly hidden, this should overall work well. The downside is that any schema changes to the table need changes to the trigger and history table, which could be slightly lost if developers don’t realize this.

    The newest evangelist at Redgate is Kendra Little, who loves triggers. Her post looks at her first experience with triggers. It was positive as a way to detect changes for downstream systems.

    Shane O’Neil has written about when triggers are good, talking about the way in which he tracks when new databases appear on his instance, with an email to auditing.

    Aaron Bertrand discusses INSTEAD OF triggers, and gives you the places where these are useful, or even necessary.

    I’ll file this as helpful when Peter Schott writes some code to disable triggers.

    Data migration is always tricky, but Jay Robinson gives a way in which triggers are helpful. I think that this is a great use, and more people ought to do this to simplify deployments and let data migrations occur over time, not all at once.

    Despite his misgivings, I think Marek Masko shows that he uses triggers effectively in code.

    I think Service Broken is amazing, and I wish that messaging and queueing processes were a pattern more of us used. Reid DeWolfe shows how triggers work in conjunction with Service Broker to get things done.

    A simple solution from Eugene Meidinger with the cautions that we need to be very careful with server level triggers.

    Headache Triggers

    Rob Farley has a discussion about some of the problems you can run into with triggers. He likes them, and has written about them before, but he offers advice for how to deal and write triggers in your organization.

    Dave Mason has a post on DDL triggers, which fire in response to event classes. If you’ve never used these, Dave gives you a few ideas on where they might be handy.

    We get a look at the good, bad, and ugly of a trigger in an AG from Bob Pusateri.

    David Fowler has a great post on nested triggers, with Service Broker in between to ensure there is no limit to the number of times the trigger fires.

    Jon Shaulis has a long post that provides an overview of triggers, where they work and don’t, and includes a few ideas for replacing triggers.

    Claudio Silva writes about triggers being hidden and wasting his time.

    I almost can’t believe the number of triggers Allen White has seen on a table, but since I have had similar customers and employers, I’m not surprised.

    Logon triggers can be headaches, and James Livingston shares one of his with us.

    The great Hugo Kornelis gives us a way to use triggers effectively and not be sad. Vote for a suggestion to make them more visible in SSMS. I voted, and I agree. We need better visibility.

    While Eduardo Pivaral writes about good and bad triggers, I think he feels triggers are somewhat bad overall. Certainly his “good fix” might not have been the best choice in his eyes, even though it worked.

    Not realizing that a trigger needs to operate on a number of records is a common mistake, so I’m putting Brian Dudley’s post in this area.

    I’d say that trigger anti-patterns fall into the headache area, and we get a few situations from Nate Johnson.

    Matthew McGiffen tells about the trouble with triggers. As with many of the other posts, he finds that triggers might be worth less than we initially think.