加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 13:28:31 所属栏目:MsSql教程 来源:网络整理
导读:??? 首先,我们创建一张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:t
??? 首先,我们创建一张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=',
   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='',
   7:  LASTROW=3,
   8:  ERRORFILE ='F:error.txt',
   9:  check_constraints)
关于BULK INSERT,请参考 MSDN。希望对您开发有帮助

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读