Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

DBD::ODBC insert first line, but not line 2

by ksublondie (Friar)
on Oct 06, 2015 at 18:05 UTC ( [id://1143958]=perlquestion: print w/replies, xml ) Need Help??

ksublondie has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on an automated script with perl 5.20 on win server 2012r2, 64-bit that will import a csv file to a sql 2008 db. Here's the code snippet:

print "Connecting to sql db..."; my $db=DBI->connect('DBI:ODBC(AutoCommit => 0):DB') or handleError({Er +rorMessage=>'DB Error'.DBI->errstr.$!,Subject=>"Transaction Import Er +ror"}); print "done\nPreparing command..."; my $statement = $db->prepare("Insert INTO Transactions VALUES (?,?,?,c +onvert(date,?),convert(date,?),?,?,?,?,?,?,?,?,?,?,?,?,?,?)") or hand +leError({ErrorMessage=>'DB Error'.DBI->errstr.$!,Subject=>"Transactio +n Import Error"}); print "Getting data\n"; my $lineno=0; while ( my $line=$csv->getline($io)){ ++$lineno; my @thisline=@{$line}; push @thisline,$thisline[13]-$thisline[16]+$thisline[17]; print join(',',@thisline)."\n"; $statement->execute(@thisline) or handleError({ErrorMessage=>'DB E +rror'."\n".$DBI::errstr."\n".$DBI::err."\n".$DBI::state."\n".$!,Subje +ct=>"GL Transaction Import Error"}); print "$lineno\n"; } $db->commit(); $db->disconnect();
and the resulting log output:

Connecting to sql db...done Preparing command...Getting data 0,10001,0,2015-10-05 12:00 AM,,74,6928.14,,POD 9085-9086 00010001 +00,,I,,7576.07,7576.07,,0,6182.34,7363.14,8756.87 1 0,10001,0,2015-10-05 12:00 AM,,44,2027.06,,POD 9075-9076 00010001 +00,,I,,7576.07,7576.07,,0,6182.34,7363.14,8756.87 calling handleError calling sendEmail Creating email...done

The email says:

DB Error [Microsoft][ODBC SQL Server Driver]String data, right truncation (SQL- +22001) 1 22001 Result too large
So it's inserting line 1, but failing on line 2. Although the content is different between line 1 & line 2, the format and length is essentially identical. If I copy/paste the command and values into management studio, the command succeeds.

Any ideas why line 1 inserts, but crashes on line 2?

UPDATE: I have modified my input file a few times, playing with the line order (ie, copy/paste line 1 so lines 1 & 2 are identical data, cut/paste line 1 down in the file, etc). No matter the data, line 1 imports, but line 2 always fails. Since it's a server 2012 box, could it be a driver issue perhaps???

UPDATE 2: I moved the $db->prepare statement from before the while loop to inside the while loop and it works. ...Of course, this defeats the purpose of prepare and execute which I've been able to do successfully in the past. Really looking like a driver issue at this point.

Replies are listed 'Best First'.
Re: DBD::ODBC insert first line, but not line 2
by jfroebe (Parson) on Oct 06, 2015 at 19:42 UTC

    The data type for at least one field is too small for the data you're trying to import. In your code, verify the data type (and length) with the data you're trying to import before you attempt to insert it.

    On a side note, why aren't you just using bcp to import your csv file? Insert statements are far slower than bcp.

    Jason L. Froebe

    Tech Blog

      The data type for at least one field is too small for the data you're trying to import. In your code, verify the data type (and length) with the data you're trying to import before you attempt to insert it.

      I've done that. All columns are varchar(50), date, or decimal(18,2) and I've tested the command in sql management studio and line 1 inserts, but not line 2. I've included the actual data being passed (in the form of the print output) in my OP.

      ...why aren't you just using bcp to import your csv file?

      Because this was SUPPOSED to be a quick script to get going. I have an existing script somewhere else on another server. Theoretically, I should just be able to copy/paste, change a few variables, and done. But no.

Re: DBD::ODBC insert first line, but not line 2
by mje (Curate) on Oct 12, 2015 at 10:30 UTC

    Assuming you are using a recent DBI and DBD::ODBC, set DBI_TRACE=DBD=x.log on the command line and rerun the script. What is in x.log?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1143958]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-23 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found