SQL Server大数据量插入

SQL Server大数据插入方法比较多,下面我们先谈谈Bulk insert 方法。

首先,我们创建一张TABLE,如下面T-SQL脚本:

 create table TestTable2
  (id int,
  amount int check(amount >=1000 and amount<=5000))
 
  

假设有这样的文本数据 testdata.txt:

1    700
2    2000
3    870
4    4500

下面这个语句不检查约束:

   1:  bulk insert TestTable2
   2:  from 'D:\testdata\testdata.txt'
   3:  with
   4:  (fieldterminator=',',
   5:  rowterminator='\n')

这个是启用约束的:

   1:  bulk insert TestTable2
   2:  from 'D:\testdata\testdata.txt'
   3:  with 
   4:  (fieldterminator=',', 
   5:  rowterminator='\n', 
   6:  check_constraints) 
   7:  select * from test 

还可以使用FIRSTROW和LASTROW限制行数。如下COPY前三行:

   1:  bulk insert TestTable2
   2:  from 'D:\testdata\testdata.txt'
   3:  with
   4:  (fieldterminator=',',
   5:  rowterminator='\n',
   6:  FIRSTROW =1,
   7:  LASTROW=3)

使用ERRORFILE选项 错误处理,如下记录到d:\error.txt

   1:  bulk insert test
   2:  from 'D:\testdata\testdata.txt'
   3:  with
   4:  (fieldterminator=',',
   5:  rowterminator='\',
   6:  FIRSTROW =1,
   7:  LASTROW=3,
   8:  ERRORFILE ='F:\error.txt',
   9:  check_constraints)

你可能感兴趣的:(数据库)