I am using Groovy SQL in a Grails application to query against a database that isn’t hooked up as the application’s datasource. By default for the page, the SQL select statement doesn’t contain an order by clause. If the user clicks on one of the tags, I want to dynamically change the query to sort on the specified column in the specified direction. When I attempt to add the order by clause to the select statement, I get a (caught) SQLException that says that my query was improperly ended.
My query:
sql.eachRow("select * from mytable where type = 'SUMMARY' and acolumn=${columnValue} order by ${sortColumn} ${sortOrder}") { row ->
results << row.toRowResult()
}
I can get around the problem by sorting the returned list however I would like to do it in the SQL statement if at all possible. Is this a known problem?
It’s Groovy trying to replace all your params with
?and making a PreparedStatementThis doesn’t work with the ORDER BY clause, so you need to use
Sql.expandTry: