hye...我有 1 个表名 rumah,其中包含 id、nama、jenis_rumah 和 sewa 字段。当 nama 像 %RIZAL% 时,我需要更新 sewa 字段,然后更新 sewa*3。如果 jenis_rumah = cornet lot 则以 20% 的折扣更新 sewa。这是我的 pl/sql,但更新过程失败...需要专家的建议。tq。
第一次审判:
DECLARE CURSOR rec IS
SELECT id, nama, jenis_rumah, sewa
FROM rumah;
nama varchar2 (100);
jenis_rumah varchar2(100);
upd_cntr NUMBER := 0;
tot_all NUMBER :=0;
BEGIN
FOR z1 IN rec LOOP
if upper (nama) like '%RIZAL%' then
begin
UPDATE rumah
SET sewa = sewa*3
where id = z1.id ;
end;
elsif jenis_rumah = 'CORNER LOT' THEN
Begin
UPDATE rumah
SET sewa = (sewa * 80/100)
where id = z1.id;
end;
END IF;
upd_cntr := upd_cntr + 1;
tot_all := tot_all + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Total Updated Sewa:' || tot_all);
END;
第二次试用:我尝试不同的方式,只有像 %RIZAL% 这样的名称被更新
DECLARE CURSOR rec IS
SELECT id, nama, jenis_rumah, SUM (sewa*3) AS upd_sewa
FROM rumah
WHERE UPPER (nama) LIKE '%RIZAL%'
GROUP BY id, nama, jenis_rumah, sewa;
jenis_rumah varchar2(100);
upd_cntr NUMBER := 0;
tot_all NUMBER :=0;
BEGIN
FOR z1 IN rec LOOP
UPDATE rumah
SET sewa = z1.upd_sewa
where id = z1.id ;
IF jenis_rumah = 'CORNER LOT' THEN
UPDATE rumah
SET sewa = (sewa * 80/100);
END IF;
upd_cntr := upd_cntr + 1;
tot_all := tot_all + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Total Updated Sewa:' || tot_all);
END;