• derekr 43208 (3/27/2012)


    Hi Guys

    By declaring the variables as below - Will that guarantee that every number generated is Unique?

    SELECT @NumberOfRows = 10000000,

    @StartValue = 1,

    @EndValue = 10000000,

    @Range = @EndValue - @StartValue + 1

    Thanks

    Yes for the rownumber generator, No for the random number generator.

    If you want unique but randomly sorted numbers, it will involve a sort (which will be time consuming for as many rows as you've identified) using NEWID() to sort on.

    WITH

    cteRowNumberGenerator AS

    (--==== Prevents sorting all the rows of the cross join.

    SELECT TOP (1000) --Put your desired number here

    UniqueNumber = ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

    FROM sys.all_columns ac1

    CROSS JOIN sys.all_columns ac2

    )--==== Randomly sorts just the numbers generated from above.

    SELECT UniqueNumber

    FROM cteRowNumberGenerator

    ORDER BY NEWID()

    ;

    --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)