Reputation: 57
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?
Upvotes: 3
Views: 1290
Reputation: 171084
It's Groovy trying to replace all your params with ?
and making a PreparedStatement
This doesn't work with the ORDER BY clause, so you need to use Sql.expand
Try:
sql.eachRow( "select * from mytable where type = 'SUMMARY' and acolumn=${columnValue} order by ${Sql.expand(sortColumn)} ${Sql.expand(sortOrder)}") { row ->
results << row.toRowResult()
}
Upvotes: 7