在sql server中一次插入固定数量的行2000

Mal*_*har 2 sql sql-server

我想一次将50,000条记录插入sql server database 2000.怎么做到这一点?

Gia*_*sti 5

您可以使用SELECT TOP子句:在MSSQL 2005中,它被扩展,允许您使用变量来指定记录数(旧版本只允许数字常量)

您可以尝试这样的事情:(未经测试,因为我目前无法访问MSSQL2005)

begin
declare @n int, @rows int

    select @rows = count(*) from sourcetable

    select @n=0

    while @n < @rows
    begin

        insert into desttable
        select top 2000 * 
        from sourcetable
        where id_sourcetable not in (select top (@n) id_sourcetable 
                    from sourcetable 
                    order by id_sourcetable)
        order by id_sourcetable

        select @n=@n+2000
    end
end
Run Code Online (Sandbox Code Playgroud)