SqlServer中BULK INSERT用法(转)
首先,我们创建一张TABLE,如下面T-SQL脚本:
1: createtable test
2: (id int,
3: amount intcheck(amount <=1000 and amount< pre>
假设有这样的文本数据:
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)