Fixed issue with SQL syntax generated from queries