|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, April 30, 2013 9:15 AM
Points: 3,
Visits: 158
|
|
Hi All,
I am having some trouble getting the following code to work, in 2005. I am trying to load a 1000 records, with one bad record, my goal is to trap the error information about that one bad record and finish the inserting the remaining 999 reords. The current code will insert 999 records, logs an error to the BULK INSERT ERRORFILE but the catch and raiserror statements do nothing.
BEGIN TRY BULK INSERT Sandbox_RTurner.dbo.TEMP_PEND_CLAIM_LINE_RT FROM 'E:\Decision Support\RTurner\temp_pend_claim_line_text.txt' WITH ( FIELDTERMINATOR = ',', TABLOCK ,ERRORFILE = 'E:\Decision Support\RTurner\PEND_CLAIM_LINE_RT'
) END TRY BEGIN CATCH DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT;
SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE();
RAISERROR (@ErrorMessage, -- Message text. @ErrorSeverity, -- Severity. @ErrorState -- State. ); END CATCH;
The following error is generated if I run the bulk insert without using the try and catch statement.
This is the error information I am trying to capture; Msg 4864, Level 16, State 1, Line 1 Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 15 (Units).
Thank you for the help
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Yesterday @ 10:13 AM
Points: 455,
Visits: 2,662
|
|
i would just add the following code into the CATCH block
SELECT ERROR_NUMBER() AS ErrorNumber , ERROR_MESSAGE() AS ErrorMessage
I would think if you want to use the RAISERROR function you would use it like this
begin try
select * from @table where 1 = 0
IF @@ROWCOUNT = 0 BEGIN DECLARE @ErrorMessage VARCHAR(100) = 'test error' RAISERROR(@ErrorMessage ,16,1) END end try
begin catch SELECT ERROR_NUMBER() AS ErrorNumber , ERROR_MESSAGE() AS ErrorMessage
end catch
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, April 30, 2013 9:15 AM
Points: 3,
Visits: 158
|
|
After reviewing raiserror some more, I do not think it is what I need here. I simply need to trap the error that is being thrown when the insert kicks out any records. I modified the code and I still am getting nothing in the catch statement.
BEGIN TRY
BULK INSERT Sandbox_RTurner.dbo.TEMP_PEND_CLAIM_LINE_RT FROM 'E:\Decision Support\RTurner\temp_pend_claim_line_text.txt' WITH ( FIELDTERMINATOR = ',', TABLOCK ,ERRORFILE = 'E:\Decision Support\RTurner\PEND_CLAIM_LINE_RT'
) END TRY
BEGIN CATCH
SELECT error_message(), error_severity(), error_state(), error_number(), error_procedure(), error_line()
END CATCH;
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Yesterday @ 6:49 AM
Points: 302,
Visits: 336
|
|
You have to include MAXERRORS option in your BULK INSERT statement. The default MAXERRORS value is 10. Since you have only one error, it was not caught by the TRY CATCH block. So, in order to capture the error in catch block you change the MAXERRORS = 0
--- Raaj
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, November 15, 2012 7:33 PM
Points: 9,
Visits: 0
|
|
|
|
|