• I made a very similar one before I re-read it and thought that he needed those numbers to exist in another table. Maybe he does, maybe not.

    DECLARE @T TABLE(

    Regionint,

    MinVint,

    MaxVint,

    NumToGenint)

    INSERT INTO @T(Region, MinV, MaxV, NumToGen)

    VALUES(1,0,5000,100),(2,1700,2300,50)

    ;WITH

    t1 AS (SELECT 1 N UNION ALL SELECT 1 N),

    t2 AS (SELECT 1 N FROM t1 x, t1 y),

    t3 AS (SELECT 1 N FROM t2 x, t2 y),

    t4 AS (SELECT 1 N FROM t3 x, t3 y),

    Tally AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) N FROM t4 x, t4 y),

    RNG AS (SELECT Region, NumToGen, MinV + ABS(CHECKSUM(NEWID()))%(MaxV-MinV) RV

    FROM @t

    )

    SELECT Region, N, RV

    FROM RNG, Tally

    WHERE N <=NumToGen

    ORDER BY Region, N

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]