Using GO Repeat To Fill Tally Table

  • Comments posted to this topic are about the item Using GO Repeat To Fill Tally Table

    Keith Wiggans

  • Perhaps is more easily as follows

    INSERT dbo.TinyTally default values

    GO 254

    instead of

    INSERT dbo.TinyTally(N) VALUES(IDENT_CURRENT('dbo.TinyTally'));

    GO

    INSERT dbo.TinyTally(N) VALUES(IDENT_CURRENT('dbo.TinyTally')+1);

    GO 254 -- Really cool tidbit I picked up from SQL Saturday #40 (will now repeat batch 254 times!)

    SET IDENTITY_INSERT dbo.TinyTally OFF;

    GO

  • Sweet!

    Keith Wiggans

  • Do you understand the difference between the two Keith? Why yours was the harder way?

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

  • Yup, I actually tried out the default values script and it works awesome. No settings, just sraight insert statements.

    Thanks for the input. Hopefully someone else gained additional understanding as well.

    Later,

    Keith Wiggans

  • Not exactly what I meant, although that is handy.

    The fact that your column is an identity column is also what does the trick. The default_values won't help you if that's not the case, especially if the column doesn't have a default specified.

    Since your column IS an identity column, you were having to say SET IDENTITY_INSERT ON before your script, and SET IDENTITY_INSERT OFF after you were done. This basically disables the automatic incrementation of the column value (if you didn't specify the IDENTITY_INSERT setting, it would have thrown an error), but in your case, this is what you want.

    Therefore, skipping the SET IDENTITY_INSERT ON and just inserting 254 times gave you the values in that column without having to do the extra work.

    Hope that helps explain a little better.

    Jon

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

  • Thanks for the script.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply