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.


In reply to DBD::ODBC insert first line, but not line 2 by ksublondie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.