I would like to ask how to simplify the following prepared statement, so it would use only 2, instead of 3 question marks (?), as for each ‘sytosc + ?’ I am setting the same value.
PreparedStatement psUp = conn.prepareStatement("UPDATE zawodnicy "
+ "SET sytosc = CASE WHEN (sytosc + ? > 100) THEN 100 ELSE sytosc + ? END "
+ "WHERE id=?");
I know in SQL you can do:
SET @a = 25;
UPDATE zawodnicy SET sytosc = CASE WHEN (sytosc + @a > 100)
THEN 100 ELSE sytosc + @a END WHERE id = 1
Obviously you cannot put it into this prepared statement since these effectively are two statements.
I wonder if there is actually a way to assign a local variable in this sort of SQL Update on first evaluation of the ‘sytosc + ?’?
I would greatly appreciate a solution compatible with MySQL as that is what I am using in this project.
1 Answer