Fixed sql statement generation