简体   繁体   中英

CASE Statement in Groovy SQL

I have some Groovy code that calls the groovy Sql object:

Sql sql = new Sql(dataSource)
sql.execute(insert)

It has worked fine in the past, but I now have some conditional logic that I tried to model with a CASE statement like CASE WHEN a.pick = ${somevalue} THEN ${100} ELSE 0 END CASE"

However, I get a runtime error that says it doesn't recognize a , but I believe it to be a symptom of the fact that it doesn't recognize the CASE statement in general, as a is recognized elsewhere in the query and is nothing new. How can I accomplish this? Thanks.

I'm guessing that this may be an issue with your mysql syntax but I can't confirm because it is not provided. Alternatively but, not that elegant; you could handle this with groovy...

def sqlInsert = "insert into foo (bar) values (${(foo.pick == someValue)? 100 : 0})"

However , first you should printing your failing query and run it in MySQL directly to see if it's a syntax issue first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM