Previous replies are both useful. Here's a way to simplify and shorten everything by using placeholders within the "IN (...)" portion of the query:
# assume that $dbh is ready... my @places = qw/Otago Auckland Northland Boolaboola/; my $query = <<ENDSQL; SELECT c_mail, c_first, c_last FROM tblClient, tblGroup, tblRegion WHERE tblGroup.g_name = 'motel' AND tblRegion.r_name IN ENDSQL my $sth = $dbh->prepare( $query . ' (' . join(',',map{'?'} @places) . +')' ); $sth->execute( @places );
This way, you don't need to worry about quoting the place names, and it might even give you more headroom in terms of overall statement length.

The @places array dictates how many placeholders are added for the "IN (...)" clause, as well as providing the placeholder values for the execute call, so if you change the number of elements in the array, the sql stuff will follow through without further ado.


In reply to Re: SQL queries using dynamic array by graff
in thread SQL queries using dynamic array by Anonymous Monk

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.