I ran into this problem this morning, and talked about a little in CB. Although I now have a workaround, I'm still wondering if anyone has had similar experiences, or has any insight into why the problem is occurring.

I'm using DBD::ADO against a SQL Server 2000 database. One of my tables has two varchar(50) columns (among others), and when I would try to insert into them, the strings were getting truncated. Here are the details.

Here's the table in question.

create table db_column ( tab_id int not null , col_num int not null , col_name varchar(50) not null , col_data_type int not null , col_data_type_name varchar(50) not null , col_charlength int , col_precision int , col_scale int , col_nullity char(1) not null , constraint db_column0 primary key ( tab_id , col_num ) )
And the statement I was using to insert data looked like this:
my $addcol = $dbh->prepare( "insert into db_column ( tab_id , col_num , col_name , col_data_type , col_data_type_name , col_charlength , col_precision , col_scale , col_nullity ) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"); # later.... $addcol->execute( $tabid, $ck->{order}, $colname, $ck->{type}, $dbh->sql_typename($ck->{type}), $ck->{charlength}, $ck->{precision}, $ck->{scale}, ($ck->{nullity}? 'Y' : 'N')) or warn "Couldn't insert into db_column: $DBI::errstr" +;
The program inserts around 4000 rows into this table in a typical run. The col_name column consistently had no more than four characters, and col_data_type_name had no more than seven. Similar truncation was happening in other tables as well.

For debugging, I printed the values just before calling execute to insert them, so I know they were correct at the time of the call. I could also insert the strings using a interactive SQL tool, so I know the table itself wasn't a problem. I ran with DBI->trace(5) so I could see the intact strings being passed down, and that there were no error messages. I poked around the http://dbi.perl.org archives but didn't find anything that seemed related to this.

The truncation went away when I stopped using placeholders. That is, when I interpolate the strings directly into the SQL statement (using $dbh->quote and friends), they make it to the database intact. I'm at a complete loss to explain it.

At this point, the modified code runs, so I'm not stuck, but I'm more curious than ever. Are there any "known issues" with DBD::ADO that I couldn't find? Does anyone see some numbskull error in my code??? As one monk pointed out, I could probably switch to DBD::ODBC and fix it also, but this smells like a bug in the ADO driver. I'll send a bug report to the author if it looks a bug to others as well.


In reply to String truncation when using DBD::ADO and placeholders by VSarkiss

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.