0

In SQL Server 2008 R2, I am trying to insert 30 million records from a source table to the target table. Out of these 30 million records, few records have some bad data and exceeds the length of target field. Generally due to these bad data, the whole insert gets aborted with "string or binary data would be truncated" error, without loading any rows in the target table and SQL Server also do not specify which row had the problem. Is there a way that we can insert rest of rows and catch the bad data rows without big impact on the performance (because performance is the main concern in this case) .

1
  • Consider setting ANSI_WARNINGS OFF "When OFF, data is truncated to the size of the column and the statement succeeds." Commented Aug 3, 2015 at 1:35

2 Answers 2

1

You can use the len function in your where condition to filter out long values:

select ...
from ...
where len(yourcolumn) <= 42

gives you the "good" records

select ...
from ...
where len(yourcolumn) > 42

gives you the "bad" records. You can use such where conditions in an insert select syntax as well.

You can also truncate your string as well, like:

select 
  left(col, 42) col
from yourtable

In the examples I assumed that 42 is your character limit.

Sign up to request clarification or add additional context in comments.

Comments

0

You are not mention that how to insert data i.e. bulk insert or SSIS.

I prefer in this condition SSIS, in which you have control and also find the solution of your issue means you can insert the proper data as @Lajos suggest as well as for bad data you can create a temporary table and get the bad datas.

You can give flow of your logic via transformation and also error handling. You can more search for this too.

https://www.simple-talk.com/sql/reporting-services/using-sql-server-integration-services-to-bulk-load-data/

https://www.mssqltips.com/sqlservertip/2149/capturing-and-logging-data-load-errors-for-an-ssis-package/

http://www.techbrothersit.com/2013/07/ssis-how-to-redirect-invalid-rows-from.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.