in reply to How to manage sql statements

It's certainly a worthy goal:

...to build towards a library of useful and hopefully generic sql statements.
Sadly, SQL doesn't admit genericity easily. SQL isn't a procedural language; that is both its strength and weakness. It doesn't admit variables (not "pure" SQL), so, for example, you can't create a "generic count-duplicates" statement like this:
SELECT @columns, COUNT(*) FROM $table GROUP BY @columns HAVING COUNT(*) > 1
where the above is a mythical SQL-Perl hybrid.

You can get halfway there. In the past, I've often resorted to managing many statements with a bit of Perl. For example, in one of my scripts to rebuild tables you'll find stuff like this:

@key_cols1 = qw(account_id group_id); @key_cols2 = qw(account_id manager_id asset_id); # Later... $set1_sql = 'SELECT ' . join(',', @key_cols1) . 'FROM ' . $table1_name . 'WHERE ' . $table1_cond # You get the idea
So, it's not pretty, but it's better to maintain one set of templates in code than 18 separate SQL files.

You're right, it seems like there should be a better way, but most SQL interpreters seem to stop just a hair short or supporting cool stuff like this. If you can come up with something workable, I'll be the first to grab your stuff!