Working with an ID column and SQL. ("newid").

  • SELECT TOP 10000 newid, *

    FROM [original-data]

    WHERE State="PE"

    ORDER BY Zip;

    The column "newid" does not actually exist in the table [original-data]. I have in mind to create the column so that the value of it for the first row is the number "1". I also have in mind to have "newid" increment automatically for every row thereafter. Is this possible to do with SQL?

    Thank You! :w00t: 😀

  • Yes it is, you could cross join the data with a numbers or tally table.

  • You can use ROW_NUMBER() OVER for this.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (5/3/2012)


    You can use ROW_NUMBER() OVER for this.

    That too. Sorry, brain was stuck on tally table as I had just been working with one.

Viewing 4 posts - 1 through 3 (of 3 total)

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