PL / SQL切换if语句

时间:2015-11-18 01:58:25

标签: plsql

如何将这个嵌套的IF语句更改为复合IF语句,我知道它几乎相同,但我不知道如何用我的代码实现它。

SET VERIFY OFF
DECLARE
   v_idno    donornew.idno%TYPE :=&input_idno;
   v_yrgoal  donornew.yrgoal%TYPE;
   v_newgoal donornew.yrgoal%TYPE;
   v_state donornew.state%TYPE;
   v_city donornew.city%TYPE;
BEGIN
   SELECT yrgoal, state, city INTO v_yrgoal, v_state, v_city
FROM donornew
WHERE idno = v_idno;
IF v_state = 'MA' AND ( v_yrgoal < 400 or v_city = 'Fall River') THEN
         v_newgoal := v_yrgoal * 2.5;
       ELSE
        v_newgoal := v_yrgoal * 1.3;
END IF;
UPDATE donornew
SET yrgoal = v_newgoal
WHERE idno = v_idno;
COMMIT;
END;
/

SET VERIFY ON

1 个答案:

答案 0 :(得分:0)

    Hello bascically for thsi purpose i will suggest you to go with purely SQL logic rather than PLSQL approach. Merge is the perfect example to resolve your issues.

    -- Approach is to use SQL approach rather then using PLSQL approach.

    MERGE INTO donornew D USING
    (SELECT yrgoal,
      state,
      city,
      idno,
      CASE
        WHEN state  = 'MA'
        AND (yrgoal < 400
        OR city     = 'Fall River')
        THEN yrgoal * 2.5
        ELSE yrgoal * 1.3
      END newgoal
    FROM donornew
    WHERE idno    = v_idno -- Input IDNO
    )A ON (d.idno = a.idno )
    WHEN MATCHED THEN
      UPDATE SET yrgoal = a.newgoal;

Let me know if this helps.