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

在sql server中一次插入固定数量的行2000,sql,sql-server,Sql,Sql Server,我想一次将50000条记录插入sql server数据库2000。如何做到这一点?你是指某种测试吗 declare @index integer set @index = 0 while @index < 50000 begin insert into table values (x,y,z) set @index = @index + 1 end 但我想这不是你的意思 如果您的意思是执行大容量插入的最佳方法,请使用大容量插入或类似bcp的方法。您是从另一个数据库/表以

我想一次将50000条记录插入sql server数据库2000。如何做到这一点?

你是指某种测试吗

declare @index integer
set @index = 0
while @index < 50000
begin
   insert into table
   values (x,y,z)
   set @index = @index + 1
end
但我想这不是你的意思


如果您的意思是执行大容量插入的最佳方法,请使用大容量插入或类似bcp的方法。您是从另一个数据库/表以编程方式插入还是从平面文件插入?

可以使用外部数据源导入数据。-b开关允许您指定批大小。

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

您可以尝试以下方法: 未经测试,因为我目前无法访问MSSQLS2005

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
将@rows声明为int 设置@rows=1 而@rows>0

开始 设置@rows=@@rowcount

结束


这是我们目前在SQL Server 2000生产中使用的代码,表和字段名已更改。

对于SQL 2000,我可能会依靠DTS来完成这项工作,具体取决于数据的位置。您可以明确地告诉DTS批提交大小使用什么。否则,SQL 2005批处理解决方案的修改版本就好了。我认为在SQL 2000中不能将TOP与变量一起使用。

不完全可以。50000行,但一次2000行。是的,我做了类似的事情。但是我没有使用NOT IN,而是使用NOT EXISTS,这应该会提供更好的性能
insert mytable (field1, field2, field3)
select   top 2000 pa.field1, pa.field2, pa.field3 
from table1 pa (nolock) 
left join mytable ta (nolock)on ta.field2 = pa.feild2
    and ta.field3 = pa.field3 and ta.field1 = pa.field1
where ta.field1 is null
order by pa.field1