• Bert De Haes (8/13/2010)


    You can easely resolve the "unique" problem by adding a distinct to the select top().

    I tried this, and it works fine on SQL 2005. The distinct will not generate an ordered list ... ( you can use newid() or rand() )

    CREATE TABLE dbo.#RandomTaxID (TaxID int);

    GO

    INSERT INTO dbo.#RandomTaxID (TaxID)

    SELECT distinct TOP (70000)

    RIGHT(CAST(CAST(CAST(NEWID() AS VARBINARY) AS BIGINT) AS VARCHAR(30)),9)

    FROM dbo.Tally;

    ALTER TABLE dbo.#RandomTaxID ADD MyID int Identity(1,1) NOT NULL primary key clustered;

    go

    -- verify uniqueness :

    create unique index UQ_RandomTaxID on #RandomTaxID(TaxID);

    select top 10 * from #RandomTaxID order by MyID;

    [font="Courier New"]

    TaxIDMyID

    514443407 1

    959020860 2

    287024448 3

    269261913 4

    575137863 5

    423204402 6

    566434316 7

    66537729 8

    82584852 9

    752384001 10[/font]

    Works just fine... until you need more than what your Tally table may have in it. Try it to gen a million numbers using a single cross join on an 11000 row Tally table. On second thought, don't try it... I stopped the code after 2.5 hours just trying to gen 70K rows.

    Using a Million row Tally, it generated 999541 rows after the distinct in about 27 seconds. Deduping appears to cost a lot on the fly both in your code and mine. 🙂

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