• To add to Luis' good posts, the Tally Table does have the limit of whatever it contains. Most people make a Tally Table to contain 11,000 or fewer rows. Some make Tally Tables with a million rows but that's still an artificial limit.

    If you want something that is only limited by the MAX value of the INT datatype, you might want to try a variation of Itzik Ben-Gan's cCTE (cascading CTE) method for generating sequences of INT's that start at 1. Here's a function to do so. I call the function "Tally1" for two reasons... 1) to keep from interfering with anyone that might have a table named "Tally" already and 2) to let me know that it'll return a sequence starting at "1". I also have functions called "Tally0" and "TallyRange" that do pretty much as they say.

    Here's the "Tally1" function.

    CREATE FUNCTION dbo.Tally1

    /**********************************************************************************************

    Purpose:

    Return a sorted column of INTs from 1 to @MaxN.

    Usage:

    SELECT t.N

    FROM dbo.Tally1(@MaxN)

    Notes:

    1. Based on Itzik Ben-Gan's cascading CTE (cCTE) method for creating a "readless" Tally Table

    source of INTs.

    2. Has been optimized for use with the INT datatype, therefor the MAX number that can be

    returned is actually 2,147,483,647. Larger numbers will return an INT Overflow error.

    Revision History:

    Rev 00 - 08 Feb 2013 - Jeff Moden - Initial creation

    **********************************************************************************************/

    (@MaxN INT)

    RETURNS TABLE WITH SCHEMABINDING AS

    RETURN

    WITH

    E1(N) AS (SELECT 1 UNION ALL SELECT 1), --2^1 or 2 Rows

    E4(N) AS (SELECT 1 FROM E1 a, E1 b, E1 c, E1 d), --2^4 or 16 Rows

    E16(N) AS (SELECT 1 FROM E4 a, E4 b, E4 c, E4 d), --2^16 or 65,536 Rows

    E32(N) AS (SELECT 1 FROM E16 a, E16 b) --2^32 or 4,294,967,296 Rows

    SELECT TOP(@MaxN) N = CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS INT) FROM E32

    ;

    Note that these types of cCTE's are very fast. Almost as fast as having an actual Tally Table. The BIG advantages are 1) they don't have an artificial limit >1 and 2) the produce absolutely "0" reads.

    Once you have that function in place (and you should put one in place... they're very useful just like a Tally Table is), then your problem becomes high-performance child's play.

    WITH cteFindMax(MaxID) AS (SELECT MAX(ID) FROM dbo.YourTable)

    INSERT INTO dbo.YourTable

    (ID, Country)

    SELECT ID = m.MaxID + t.N,

    Country = 'Germany'

    FROM cteFindMax m

    CROSS APPLY dbo.Tally(1002261 - m.MaxID)t

    ;

    Shifting gears a bit, if you didn't know what the new MAX was that you wanted to have but knew you wanted to add, say, 100 rows, that's even easier.

    INSERT INTO dbo.YourTable

    (ID, Country)

    SELECT ID = (SELECT MAX(ID) FROM dbo.YourTable) + t.N,

    Country = 'Germany'

    FROM dbo.Tally(100)t

    ;

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