使用日期名称值更新 table - Oracle pl/sql

Update table using day name value - Oracle pl/sql

我是这个论坛和 oracle pl/sql 的新手。从最近几天开始,我一直在做一些 pl/sql 任务。其中一项任务是更新修订金额列中的金额值。我正在为此任务使用游标方法。

我需要按 +10(星期一)、+20(星期二)等更新修订金额列的值。

我在更新修改后的金额时遇到问题。

请检查以下代码。

create or replace procedure team2
as 
  cursor c1 is select amount, day_name from football;
  v_amount football.amount%type;
  v_day football.day_name%type;
begin
  if c1%isopen then
    close c1;
  end if;

  open c1;
  loop
    fetch c1 into v_amount, v_day;
    exit when c1%notfound;

    if  v_day = 'monday' then
      update football set revised_amount = v_amount+10 where day_name = v_day ;
    elsif v_day = 'tuesday' then
      update football set revised_amount = amount+30 where day_name = v_day ;
    elsif v_day = 'wednesday' then
      update football set revised_amount = amount+40 where day_name = v_day ;
    elsif v_day = 'thursday' then
      update football set revised_amount = amount+50 where day_name = v_day ;
    elsif v_day = 'friday' then
      update football set revised_amount = amount+60 where day_name = v_day ;
    elsif v_day = 'saturday' then
      update football set revised_amount = amount+70 where day_name = v_day ;
     elsif v_day = 'sunday' then
       update football set revised_amount = amount+80 where day_name = v_day ;
     else
       dbms_output.put_line(' output failed');
     end if;
  end loop;
  close c1;
End;
/

尝试以下方法:

update football
  set revised_amount = amount + case day_name when 'monday' then 10 when 'tuesday' then 20 ... when 'sunday' then 80 end
;

仅限星期一:

update football
  set revised_amount = amount + case day_name when 'monday' then 10 end
where day_name in ('monday')
;

你应该不需要游标:

create or replace procedure team2
as 
begin
  UPDATE Football
  SET    revised_amount = amount
                        + DECODE(
                            day_name,
                            'Monday',    10,
                            'Tuesday',   20,
                            'Wednesday', 30,
                            'Thursday',  40,
                            'Friday',    50,
                            'Saturday',  60,
                            'Sunday',    70
                          );
END;
/

(而且你真的不需要这个程序)