#730: Restructure SQL query substitution

Resolves #730.