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

I am trying to use the following code to create a query that uses the sql "LIKE" operator but i am not seeing expected results.

my $sql = SQL::Abstract->new(cmp => 'like'); #my $sql = SQL::Abstract->new(case => 'lower', cmp => 'like'); my @fields = qw/company_number short_company_name country_code long_co +mpany_name company_stock_symbol_description company_state_province_name company_c +ity_name company_web_address_description/; # check hash for values first! my($stmt, @bind) = $sql->select('company', \@fields, \%HoH); my $sth = $dbh_essweb->prepare($stmt); $sth->trace(4, "Xtrace.log"); $sth->execute(@bind);

I want he sql to look like:

Select blah from tablename where column_name like '%search pattern%'
I am affraid the SQL::abstract module is not generating the percent signs around the search pattern.

Can anyone give me some tips on what i am doing wrong? Thanks

Replies are listed 'Best First'.
Re: SQL::Abstract not working right
by jdtoronto (Prior) on Oct 10, 2006 at 15:52 UTC
    data67,

    Why would you want some default behaviour of the LIKE? If you look in the SQL::Abstract doc's you will find:

    But, this is probably not what you want in this case (look at it). So +the hashref can also contain multiple pairs, in which case it is expa +nded into an AND of its elements: my %where = ( user => 'nwiger', status => { '!=', 'completed', -not_like => 'pending%' } ); # Or more dynamically, like from a form $where{user} = 'nwiger'; $where{status}{'!='} = 'completed'; $where{status}{'-not_like'} = 'pending%'; # Both generate this $stmt = "WHERE user = ? AND status != ? AND status NOT LIKE ?"; @bind = ('nwiger', 'completed', 'pending%');
    Which indicates that you need to provide your own wild-cards. The module will HELP you create complex SQL, but it doesn't do absolutely everyting for you. In time you may in fact be thanksful it works this way. I am :)

    jdtoronto

      Thank you!