Thursday, April 4, 2013

ERRORFILE and MAXERRORS option with BULK INSERT


In the below code MAXERRORS argument defines the total number of records which can be rejected before entire file will be rejected by the system. It is a tolerant level and can be any integer number based on your discretion and requirement
ERRORFILE will define the name and path of the error log file which will contain the erroneous records rejected during Bulk Insert. It will contain maximum of MAXERRORS+1 records.
So in the above script complete file will be rejected only if more than 500 records are rejected during BULK INSERT and 501 records will be logged into Error log file which will be present at the path defined in ERRORFILE argument.
While if less than 500 records are rejected (or no records are rejected) than those records will be logged into error log file while rest of the records will be successfully loaded into the staging table.

Conclusion :


Although these two arguments are rarely used but they can be very helpful if you want to know about the records which are rejected and also to set the tolerance level of the file loaded by defining MAXERRORS value.
DECLARE @SQL   varchar(2000)
DECLARE @FileToLoad   varchar(100)
DECLARE @DestinationTableName   varchar(50)
DECLARE @StartingRow   int
DECLARE @FormatFile  varchar(50)
DECLARE @ErrorLogFile  varchar(50)
SET @FileToLoad = '\\servername\foldername\test.txt' --This is the name and path of the file which is to be BULK INSERTED into the staging table
              
SET @DestinationTableName = 'STAGING_TABLE' --Name of the staging table into which data from .txt file need to be loaded
        
SET @StartingRow = 2 --Tell us about the starting row in text file which needs to be loaded into staging table. 1st row is left as header
                 
SET @FormatFile = '\\servername\Format_file\test.fmt' --path and name of the format file.Format file with extension.fmt is used to define the mapping between text file columns and staging table columns
              
SET @ErrorLogFile  = '\\servername\TestErrorLog.txt' --path and name of the errorlog file.Records rejected during Bulk Insert will be saved in this error log file                                     

 SELECT @SQL = 'BULK INSERT DATABASENAME.dbo.' + @DestinationTableName + ' FROM ''' + @ FileToLoad + '''
   WITH ( FIRSTROW = ' + RTRIM(STR(@StartingRow)) + ',
    MAXERRORS = 500,
    FORMATFILE = ''' + @FormatFile + ''',
    ERRORFILE =''' + @ ErrorLogFile + ''',
    ) '
EXECUTE (@SQL)

No comments:

Post a Comment