When that happens to me I changeINSERT INTO MyDestinationTable( Col1, Col2, ...)SELECT Col1, Col2, ...FROM MySourceTable
intoSELECT Col1, Col2, ...INTO #TempTableFROM MySourceTableSELECT [Col1MAX] = MAX(DATALENGTH(Col1)), [Col2MAX] = MAX(DATALENGTH(Col2)), ...FROM #TempTable
(only for the Varchar columns ) and then see which column has a width that is too wide. You can skip straight to the next step - finding which row it isSELECT Col1, Col2, ..., [ErrorMessage] = CONVERT(varchar(8000), NULL)INTO #TempTableFROM MySourceTableUPDATE USET ErrorMessage = COALESCE(ErrorMessage+'; ', '') + 'Col1 too long [' + CONVERT(varchar(20), DATALENGTH(Col1)) + ']'WHERE DATALENGTH(Col1) > 1234 -- Need the actual Max Width of COL1 here... Repeat for Col2, Col3, (all varchar columns) ...-- Display any errorsSELECT *FROM #TempTableWHERE ErrorMessage IS NOT NULL
and if you want to insert all rows with NO errors, anyway, then you can do:INSERT INTO MyDestinationTable( Col1, Col2, ...)SELECT Col1, Col2, ...FROM MySourceTableWHERE ErrorMessage IS NOT NULL
although you need to be sure that the actual errors are reported to someone who can sort them out AND there is a means of then selectively importing those later (for example if there is a daily import of "everything" or "everything that has changed" then reporting the errors to an operators to fix, before tomorrow's run, might be good enough.You can add any number of tests for other goofy data - illegal dates, MAX value smaller than corresponding MIN value, Names that fail a Lookup to a Contacts table, all that sort of data-validation jazz. Anything that adds an entry to the ErrorMessage column will prevent the subsequent INSERT of the junk data.Add a WarningMessage column too, if there are any non critical errors. Import those rows anyway (provided ErrorMessage IS NULL) but include them in the error report for the operator to investigate. "Order date set to a future date!!" for example, I've had that happen when a computer's clock has been set wronly