While
serf solved your immediate problem, I'd like to offer an alternate solution. This one uses a couple other DBI features which are useful, better SQL features, plus formats the SQL in a way I really like that a DBA once showed me.
my $sth = $dbh->prepare( <<'__END_SQL__' );
SELECT smt.uniqueid
,smt.creationtime
,att.name
,att.val
,gsm.msisdn
FROM dagenericsmt smt
JOIN dagenericsmtattribute att ON smt.uniqueid = att.genericsmt_uniq
+ueid
JOIN dagsmproduct gsm ON smt.product_uniqueid = gsm.uniqueid
WHERE smt.smtstatus = ?
AND smt.smtcommand = ?
AND smt.acktime IS NULL
AND att.name IN (?,?)
ORDER BY smt.uniqueid
__END_SQL__
$sth->execute( 2, 3, 'GSM/Cur/IMSI', 'GSM/Rpl/IMSI' );
Explanations:
- I'm using Perl formats to lay out the SQL statement the way it would be laid out by a DBA in a .sql file. This allows you to cut'n'paste the SQL statement to an email to a DBA and not have to reformat it. Unless the DBA really likes you, s/he won't even look at what you have in your version. They have enough to do.
- I'm using DBI placeholders. They give you two things:
- They allow you to reuse the same $sth for multiple values, avoiding additional statement parses. This reduces the load on both the application server and (potentially) the database server.
- They allow DBI to quote your strings, thus making SQL injection attacks impossible.
- I'm using SQL's JOIN feature vs. doing the table JOINS in the WHERE clause. This does two things:
- It's now easier to see what's involved with joins and what's involved with restricting the set to be returned. Before, it was just a jumbled mash.
- Most databases (including Oracle) can process a query more efficiently if they see the joins separate from the WHERE clause.
- All the SQL keywords are in uppercase, making it much easier to understand what's happening. Also, everything is indented nicely, meaning that your eye has to do less work to see what's going on.
Yes, it takes a ton more lines. It's also more maintainable. If you had to make a change to the query, would you rather do it to yours or mine?
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.