How to automatically truncate string when do bulk insert?

Quan Mai picture Quan Mai · Jun 19, 2011 · Viewed 8.4k times · Source

I want to insert many rows (constructed from Entity Framework objects) to SQL Server. The problem is, some of string properties have length exceeded length of column in database, which causes an exception, and then all of rows will unable to insert into database.

So I wonder that if there is a way to tell SqlBulkCopy to automatically truncate any over-length rows? Of course, I can check and substring each property if it exceeds the limited length, before insert it in to a DataTable, but it would slow down the whole program.

Answer

gbn picture gbn · Jun 19, 2011

Always use a staging/load table for bulk actions.

Then you can process, clean, scrub etc the data before flushing to the real table. This includes, LEFTs, lookups, de-duplications etc

So:

  1. Load a staging table with wide columns
  2. Flush from staging to "real" table using INSERT realtable (..) SELECT LEFT(..), .. FROM Staging