I don't know how many monks use DBD::Sybase, but I've just been informed of a potentially nasty bug with DBD::Sybase when using placeholders in conjunction with a LIKE clause.

For example:

use DBI; my $param = shift; my $dbh = DBI->connect('dbi:Sybase:database=testdb', 'sa', ''); my $sth = $dbh->prepare("select * from CAL where CAL_DS like ? + '%'") +; $sth->execute($param); while(my $d = $sth->fetch) { print "@$d\n"; }
Assume a CAL table with the following rows:
id CAL_DS -------- -------------------- 1 A 2 AM 3 AMS 4 AMSTEL 5 DSTEL
Running the script with any argument starting with 'A' will return the first 5 rows (so $sth->execute('ABC') and $sth->execute('A') have the same effect).

I've traced this to a bug in the Sybase OpenClient libraries, where the input parameter description returns a parameter length of 1 for this query, even though the CAL column is a varchar(20).

The only work-around that I can think of is to move the '%' character from the SQL string to the parameter (i.e. $sth->execute('AMS%')) which will work as expected, but is not a generic solution for LIKE queries if you always want to have the wildcard in the query.

Michael

Replies are listed 'Best First'.
Re: DBD::Sybase bug
by VSarkiss (Monsignor) on Mar 12, 2002 at 20:09 UTC

    I just tried it with ASE 11.9.2 on HP-UX 11.0, Perl 5.6.1, DBD::Sybase 0.93, and saw the bug.

    I used your work-around and put the % character in the execute call:

    my $sth = $dbh->prepare("select * from CAL where CAL_DS like ?"); $sth->execute($param . '%');
    I confirmed that worked as expected.

    Update
    Removed nonsensical sentence....