当前位置: 代码迷 >> Sql Server >> sql插入6W多条数据,该怎么解决
  详细解决方案

sql插入6W多条数据,该怎么解决

热度:63   发布时间:2016-04-24 09:31:49.0
sql插入6W多条数据
数据库只有一张表 Acc_Num 只有一个字段num

向里面插入6w条数据...

从1-60000插入。怎么插入是最快的。

我实在不知道怎么插入,我唯一想到的就是while循坏...一直循坏60000次...

------解决思路----------------------
if exists(select 1 from sys.objects where type='U' and name='Acc_Num') drop table Acc_Num
go
select top 60000 num=identity(int,1,1)into Acc_Num from syscolumns a,syscolumns b
go
select * from acc_num

------解决思路----------------------
DECLARE @maxNum int

INSERT INTO Acc_Num VALUES(1)
SET @maxNum = 1

WHILE @maxNum < 60000
BEGIN
    INSERT INTO Acc_Num
    SELECT @maxNum + num
      FROM Acc_Num
     WHERE @maxNum + num <= 60000

    SET @maxNum = @maxNum*2
END

16次INSERT搞定。
  相关解决方案