Just a note on WHERE column LIKE 'something%', if this is really what you want:

Using LIKE with only a trailing % is usually slower than using SUBSTR, or at least it was WAY slower the last time I had to do serious work with relational databases. It boils down to the query optimizer not being able to use indices on the queried column with LIKE, resulting in a full table scan. If you use SUBSTR instead, your query can be optimized to use indices and thus does not have to scan the entire table.

So, unless SQL query optimizers have become much smarter than they were a few years ago, WHERE SUBSTR(column, 1, 9) = 'something' should be faster.


Using DBI, there are basically three ways to pass the length of the search term to SUBSTR:

If you can use numbered or named placeholders (depends on your database driver), you simply reuse the placeholder:

# numbered: my $sth=$dbh->prepare('SELECT ... FROM ... WHERE SUBSTR(column, 1, LEN +GTH(:1)) = :1'); $sth->bind_param(1, $what); $sth->execute(); # named: my $sth=$dbh->prepare('SELECT ... FROM ... WHERE SUBSTR(column, 1, LEN +GTH(:what)) = :what'); $sth->bind_param(':what', $what); $sth->execute();

If you are limited to simple ? placeholders, you can pass the same value twice:

my $sth=$dbh->prepare('SELECT ... FROM ... WHERE SUBSTR(column, 1, LENGTH(?)) = ?'); $sth->bind_param(1, $what); $sth->bind_param(2, $what); $sth->execute(); # or without explicit bind_param: $sth->execute($what, $what); </c>

You could also calculate the length in perl. This should normally work, unless your data contains Unicode AND perl, DBI, DBD, and the database don't agree on how your data is really encoded (i.e. you messed up the Unicode handling):

my $sth=$dbh->prepare('SELECT ... FROM ... WHERE SUBSTR(column, 1, ?) += ?'); $sth->execute(length($what), $what);

Note: This will cause interesting results if Unicode handling is messed up ...


Oh, by the way: What happens if $transactionCodeQuery contains a %? If you can guarantee it does not and will never, don't mind. If $transactionCodeQuery is user input, try harder to avoid using LIKE. If there is no way around LIKE, try using ... LIKE ... ESCAPE ....

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re: Perl DBI query of MS SQL with LIKE by afoken
in thread Perl DBI query of MS SQL with LIKE by gcasa

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.