Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditional update using HQL

I'm trying to update some rows of my database using the following script which works pretty fine :

update DOS
set NAMEDOS=:name,
AGEDOS=:age,
WEIGHTDOS=:weight
where CODEDOS=:code

My problem is the following, sometimes the weight can be empty or null, so i have to just set the other properties.

I have tried to proceed like this, but it doesn't seems to work:

update DOS
set NAMEDOS=:name,
AGEDOS=:age,
WEIGHTDOS= (case when weight is not null then :weight else :WEIGHTDOS end),
where CODEDOS=:code

Can you help me please.

like image 761
makassi wlefred Avatar asked Oct 22 '25 00:10

makassi wlefred


1 Answers

Try switching the brackets a bit and i think that :WEIGHTDOS should be without the double dots as its the direct column name not a parameter:

update DOS
    set NAMEDOS=:name,
    AGEDOS=:age,
    WEIGHTDOS= case when (:weight is not null) then :weight 
                  else WEIGHTDOS 
               end,
    where CODEDOS=:code
like image 131
Maciej Kowalski Avatar answered Oct 24 '25 14:10

Maciej Kowalski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!