Tag: syndicated

  • Identity Reseeding

    I love the identity property. I use it in many of my tables, mostly because it gives me a fairly reliable surrogate key that I can use in my tables, especially when testing something. I often do something like this:

    CREATE TABLE MyIdentityTest
    ( id INT IDENTITY(1,1)
    , mychar VARCHAR(10)
    )
    GO
    INSERT MyIdentityTest SELECT 'A'
    INSERT MyIdentityTest SELECT 'B'
    INSERT MyIdentityTest SELECT 'C'
    GO

    In this table, I have these results:

    id          mychar

    ———– ———-

    1           A

    2           B

    3           C

    If I then look to reseed things for some reason, maybe I want to leave a gap somewhere, I can do this:

    SET IDENTITY_INSERT MyIdentityTest ON
    INSERT MyIdentityTest (id, mychar) SELECT 12, 'L'
    GO
    SET IDENTITY_INSERT MyIdentityTest OFF
    GO

    This means my table now looks like this.

    id          mychar

    ———– ———-

    1           A

    2           B

    3           C

    12          L

    If I insert a new value:

    INSERT MyIdentityTest SELECT 'M'
    GO
    

    And now check all the results, I have this:

    SELECT * FROM myidentitytest

    we get

    id          mychar

    ———– ———-

    1           A

    2           B

    3           C

    12          L

    13          M

     

    Suppose I realized that I had a problem and decided to “fix” my identity values. I can reseed like this, which sets the identity property tracker back to 3.

    DBCC CHECKIDENT(myidentitytest, RESEED, 3)
    go
    INSERT MyIdentityTest SELECT 'D'
    GO

    However look at the results of the insert. It shows 4 instead of 3, which is what I want in this case.

    SELECT * FROM myidentitytest

    id    mychar

    —– ———-

    1     A

    2     B

    3     C

    12    L

    13    M

    4     D

    Quite a few people think that if I set the identity to “3”, I should have 3 as the next value. That’s not the case, and it’s something to be aware of when working with identities. If you are looking to fill gaps or move your seed for some reason (like merge replication), understand that you are inserting the “last” value as your seed, not the “next” one.

  • Better Writing from Hemingway

    These are five rules for writing from Ernest Hemingway, which I like and recommend for use in your communications. Definitely try to incorporate them into your writing, especially being short and positive.

    (Cross posted from The Modern Resume)

  • More Bloopers for Memorial Day

    It’s Memorial Day, and as usual, I have a blooper reel for the editorial. However I couldn’t fit everything in there, so I’ve pulled out a few more items. Mostly body mistakes or movements and some speaking issues. Enjoy!

    The editorial blooper reel is here: http://m.podshow.com/media/15351/episodes/282544/sqlservercentral-282544-05-26-2011.mp4

  • No Shrink-y Tempdb

    I caught the end of a Twitter conversation the other day between Wes Brown (Blog | @WesBrownSQL), Paul Randal (blog | @PaulRandal), and Aaron Bertrand (blog | @aaronbertrand) talking about shrinking tempdb and the potential issues.

    I’ve never thought about it before, but someone investigated and the question popped up on Twitter. Nioholas Cain (blog | @SirSQL) wrote a nice blog post on the events, and it explains how he came to the question (a full disk).

    The short answer is that you can cause corruption in tempdb if there is activity taking place when you shrink.

    So don’t shrink tempdb!

    At least not without quiescing SQL Server. If you don’t know what that means, don’t shrink tempdb on your active server. In case you don’t know, it means allowing active connections to drain off and not allowing new ones. There’s a KB article on this.