In the SQL Server forums, I see questions complaining that BULK INSERT is not taking care of check constraints. There are also some other questions related to BULK INSERT. Here I summarise them and explain
1 Enabling Check contraint - Use the option check_constraints
create table test
(id int,
amount int check(amount >=1000 and amount<=5000));
Consider the following data in the text file
1 700
2 2000
3 870
4 4500
The following query will not check the constraint
bulk insert test
from 'f:\test.txt'
with
(fieldterminator=',',
rowterminator='\n')
To enable the check constraint specify the option check_constraints
bulk insert test
from 'f:\test.txt'
with
(fieldterminator=',',
rowterminator='\n',
check_constraints)
select * from test
2 Limiting the rows - Use the options FIRSTROW and LASTROW.
This copies the first three rows
bulk insert test
from 'f:\test.txt'
with
(fieldterminator=',',
rowterminator='\n',
FIRSTROW =1,
LASTROW=3)
3 Error handling - Use the option ERRORFILE
The row terminator is not correct. So an error file is generated
bulk insert test
from 'f:\test.txt'
with
(fieldterminator=',',
rowterminator='\',
FIRSTROW =1,
LASTROW=3,
ERRORFILE ='F:\error.txt',
check_constraints)
Also refer these posts about BULK ISERT
http://beyondrelational.com/blogs/madhivanan/archive/2010/03/17/bulk-insert-to-table-with-specific-columns.aspx http://beyondrelational.com/blogs/madhivanan/archive/2010/12/15/bulk-insert-comma-as-part-of-data.aspx