in reply to DBI - Get prepared/populated sql statement

Random tip. I prefer to use the qq{} syntax so that the interpolated string doesn't stick out from my code. I also have a slightly different standard for writing SQL. So a basic example could look like this:
my $sth = $dbh->prepare(qq{ select tbl.column1 , tbl2.column2 from tbl1 join tbl2 on tbl1.tbl2_id = tbl2.id where condition = ? and condition2 = ? }) or die "Can't prepare query: $DBI::errstr";
The basic idea being that continuation pieces like "," and "and" go in front of the next line rather than trailing on the previous line. The point being that when you go to change your SQL you're likely to add columns and/or conditions, and with my way of arranging things you avoid the annoying error where you insert a bunch of rows but forgot to add the continuation piece just before your insert.

You don't have to do things that way, but if you write as much SQL as I do, you'll find that it makes sense.

Update: zwon pointed out that I missed the qq{} in the code even though I had just talked about it. D'oh, fixed.

Replies are listed 'Best First'.
Re^2: DBI - Get prepared/populated sql statement
by mhearse (Chaplain) on Mar 10, 2009 at 17:42 UTC
    Thanks for your tip. I do write a great deal of SQL. After leafing through some of Joe Celko's books, there's a lot more for me to learn!