简体   繁体   中英

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.

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

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