March 18, 2010 at 8:23 am
Hi
I have a SQL database and tables are inserted overnight.
I have a record in this table (see attached) that is throwing the above error when I try and insert into SQL.
Can anyone advise how I can find the narrow down and find the problem record?
And how I could then go about ensuring the insert works .
Many thanks
March 18, 2010 at 4:28 pm
One rather brute force method would be to write a cursor that inserts one record at a time and then capture the error/identify the record via try/catch.
March 18, 2010 at 7:03 pm
I'm guessing you 're trying to insert data in the wrong format.
Most probably it's caused by the format you're trying to insert conflicting with the current setting of DATEFORMAT.
Example:
SET DATEFORMAT DMY
--will work
INSERT INTO a SELECT '12-13-2010'
--won't work
INSERT INTO a SELECT'13-12-2010'
If this doesn't help to solve the issue please post the full error message together with some sample data in SQL format (=INSERT INTO ... SELECT...) instead of an attached Excel file.
March 19, 2010 at 8:00 am
Insert the data into a staging table with all VARCHAR() columns. Use ISDATE and couple of other checks on the date columns to validate the date BEFORE you try to insert it into the final table. Write all "bad" rows to an "errata" table for later investigation and repair.
As a sidebar, you should never import data to final tables... always import to a staging table and do your validations there.
As another sidebar, there are "switch settings" in both BCP and Bulk Insert (as of 2k8) that will allow you to basically "ignore" error rows and capture them in an "errata" file. If the staging table has a DateTime datatype and a bad date comes in, the bad-date-row (and possibly the next rows) will automatically be routed to an errata file. Of course, that will work fine for direct inserts into the final table, as well, but I'd get out of that habit. Like I used to tell my step-son... you don't know where that's been... don't put it in your mouth until you wash it. 😛
--Jeff Moden
Change is inevitable... Change for the better is not.
March 19, 2010 at 8:07 am
I think Jeff has what I'd suggest. Get it staged, then clean it up
March 19, 2010 at 9:15 am
You could also use an SSIS package.
It has built-in features to do almost all of the stuff Jeff is suggesting, just by connecting a few dots.
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply