Oracle - 根据条件更新一列或另一列

z-d*_*dan 5 sql oracle

我想更新表中的记录,但根据条件,我将更新一列或另一列,但我不想有2个单独的语句,因为这些语句非常冗长和详细.

以下是过度简化的基本思路.

PROCEDURE Animal_something(p_updater VARCHAR2)

begin

  if p_updater = 'person' then   
    -- I want to update the modified_by  
  else   
    -- if p_updater = 'a process' I want to update modified_by_process

Update table_creatures
   set animal_type = 'Dog ,

**modified_by** = 'Bob'   
**or do this**  
**modified_by_process =** 'creature_package'

 where animal_legs = '4'
Run Code Online (Sandbox Code Playgroud)

不想要:

if p_updater = 'person' then 
  Update table_creatures   
     set animal_type = 'Dog ,  
         modified_by = 'Bob'  
   where animal_legs = '4';  
else  

  Update table_creatures  
     set animal_type = 'Dog , 
         modified_by_process = 'creature_package'  
   where animal_legs = '4';

end;
Run Code Online (Sandbox Code Playgroud)

Qua*_*noi 7

UPDATE  table_creatures
SET     animal_type = 'Dog',
        modified_by = CASE p_updater WHEN 'person' THEN 'Bob' ELSE modified_by END,
        modified_by_process = CASE p_updater WHEN 'process' THEN 'creature_package' ELSE modified_by_process END
WHERE   animal_legs = 4
Run Code Online (Sandbox Code Playgroud)