SQL Server:如何使用重复序列1-12创建列?

时间:2015-07-02 17:41:23

标签: sql sql-server-2012

长话短说,我想创建一个重复1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4模式的列, ...等等。 for(12 * 460343 =)5524116行。关于如何完成这个的任何智慧?谢谢!

2 个答案:

答案 0 :(得分:3)

插入48然后从self中选择几次。你会真正快速到达那里。它比人们想象的要快得多。

如果您创建一个带有int autoinc列的表,那么最后:

delete from table where id>5524116

在这里编辑

    create table idFix
    (   id bigint auto_increment primary key,
        num int not null
    )engine=myisam;

    -- prime it    
    insert into idFix(num) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12);

    -- this is pretty fast, don't laugh
    -- run the following line 19 times
    insert into idFix(num) select num from idFix;

    -- you now have 6.2m rows (6,291,456)
    select count(*) from idFix

    delete from idFix where id>5524116;

    select count(*) from idFix;

    select min(num),max(num) from idFix;

Takes 3 minutes max

Use your helper table then for the love of Pete drop it !

答案 1 :(得分:1)

使用循环并与计数器进行一些mod分割。

DECLARE @LoopCounter bigint
SET @LoopCounter = 0

CREATE TABLE #YourValues
(
    YourValue_Key int NOT NULL identity (1,1) PRIMARY KEY, 
    YourValue_OneThrough12Repating int
)

WHILE @LoopCounter < 5524116
    BEGIN
        INSERT INTO #YourValues (YourValue_OneThrough12Repating) VALUES ((@LoopCounter % 12) + 1)
        SET @LoopCounter = @LoopCounter + 1
    END

SELECT * FROM #YourValues

DROP TABLE #YourValues