SQL statements are just pieces of text. If you're looking to create templated text, you will want to break the targets up into what's shared and what differs. Then, build your pieces as needed.
Often, I will use HTML::Template to build template files. I had very good success with this when I needed to build auditing tables in Oracle. Don't be fooled by the HTML:: part - HTML::Template is an excellent generic templating solution. It just so happens that its most common usage is HTML.
Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.