SqlServer中BULK INSERT用法简介,批量插入数据

    首先,我们创建一张TABLE,如下面T-SQL脚本:
   1:  create table test
   2:  (id int,
   3:  amount int check(amount >=1000 and amount<=5000));

假设有这样的文本数据:

1    700
2    2000
3    870
4    4500

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

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

这个是启用约束的:

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

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

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

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

   1:  bulk insert test
   2:  from 'f:\test.txt'
   3:  with
   4:  (fieldterminator=',',
   5:  rowterminator='\',
   6:  FIRSTROW =1,
   7:  LASTROW=3,
   8:  ERRORFILE ='F:\error.txt',
   9:  check_constraints)
关于BULK INSERT,请参考 MSDN。希望对您开发有帮助

你可能感兴趣的:(java,SqlServer)