0

我有一个存储过程,提示用户输入并存储/更新我的表。我的问题是,当尝试在输入数字时添加限制时,它不能按计划工作。

这是表格:

       Drop table incident;
       CREATE TABLE INCIDENT 
       (ICD_ID VARCHAR2 (8) NOT NULL PRIMARY KEY,
       PLR_ID VARCHAR2 (11),
       M_ID VARCHAR2 (10),
       PP_ID VARCHAR2 (10),
       I_POINTS NUMBER (8));


       INSERT INTO INCIDENT VALUES
       ('ICD01', 'CHE01', 'M01', 'PP01', NULL);

       INSERT INTO INCIDENT VALUES
       ('ICD02', 'CHE03', 'M07', 'PP02', NULL);

       INSERT INTO INCIDENT VALUES
       ('ICD03', 'CHE03', 'M04', 'PP03', NULL);

       INSERT INTO INCIDENT VALUES
       ('ICD04', 'KLN04', 'M07', 'PP02', NULL);

       INSERT INTO INCIDENT VALUES
       ('ICD05', 'CHE01', 'M04', 'PP03', NULL);

出现提示时,我输入 ICD_ID (例如 'ICD03') *必须是大写* 条目。

步骤:

   DECLARE 
   v_icd_id incident.icd_id%type := &INCIDENT_ID; 
   v_inc_I_POINTS incident.i_points%type := &PENALTY_POINTS; 
   v_I_POINTS incident.I_POINTS%type;
   V_PP_ID INCIDENT.PP_ID%TYPE;    
   E_TOO_MANY_POINTS exception;
   E_UPDATE_HIGH exception;   
   E_A exception;
   E_B exception;
   E_C exception;
   BEGIN 
   SELECT I_POINTS INTO v_I_POINTS 
   FROM incident WHERE icd_id = v_icd_id;

   IF v_I_POINTS > 10 THEN 
   RAISE E_UPDATE_HIGH; 
   END IF;

   IF v_inc_I_POINTS >10 THEN
   RAISE E_TOO_MANY_POINTS; 
   END IF;


   IF V_PP_ID = 'PP03' OR V_INC_I_POINTS > 10 OR V_INC_I_POINTS < 10 THEN
   RAISE E_A;
   END IF;


  UPDATE incident SET i_points =  (v_inc_i_points + NVL(i_points,0))               
   WHERE icd_id=v_icd_id;



  DBMS_OUTPUT.PUT_LINE('');
  DBMS_OUTPUT.PUT_LINE('=====================');
  DBMS_OUTPUT.PUT_LINE('PENALTY POINTS UPDATED!');
  DBMS_OUTPUT.PUT_LINE('=====================');
  DBMS_OUTPUT.PUT_LINE('');

  EXCEPTION 
  WHEN E_TOO_MANY_POINTS THEN 
  DBMS_OUTPUT.PUT_LINE('==========================================================');
  DBMS_OUTPUT.PUT_LINE('TOO MANY POINTS ALLOCATED THERE! ACTION ABORTED.');
  DBMS_OUTPUT.PUT_LINE('==========================================================');


  WHEN E_UPDATE_HIGH THEN 
        DBMS_OUTPUT.PUT_LINE('=============================================================');
  DBMS_OUTPUT.PUT_LINE('CAN NOT STORE THAT MANY PENALTY POINTS THERE! ACTION ABORTED.');
    DBMS_OUTPUT.PUT_LINE('=============================================================');


  WHEN E_A THEN 
  DBMS_OUTPUT.PUT_LINE('=============================================================');
  DBMS_OUTPUT.PUT_LINE('CAN ONLY ALLOCATED 10 POINTS HERE! ACTION ABORTED.');
  DBMS_OUTPUT.PUT_LINE('=============================================================');


  WHEN E_B THEN 
  DBMS_OUTPUT.PUT_LINE('=============================================================');
  DBMS_OUTPUT.PUT_LINE('CAN ONLY ALLOCATED 5 POINTS HERE! ACTION ABORTED.');
  DBMS_OUTPUT.PUT_LINE('=============================================================');

  WHEN E_C THEN 
  DBMS_OUTPUT.PUT_LINE('=============================================================');
  DBMS_OUTPUT.PUT_LINE('CAN ONLY ALLOCATED 1 POINTS HERE! ACTION ABORTED.');
  DBMS_OUTPUT.PUT_LINE('=============================================================');

 END;
  /

我要完成的工作:

是为了防止人们在 PP_ID 为“PP03”的情况下输入大于 10 且小于 10 的值。

为此,我申请了:

   IF V_PP_ID = 'PP03' OR V_INC_I_POINTS > 10 OR V_INC_I_POINTS < 10 THEN
   RAISE E_A;
   END IF;

这很好用,但是当我添加另一个限制时:

为了防止人们在 PP_ID 为“PP02”的情况下输入大于 5 且小于 5 的值。

    IF V_PP_ID = 'PP02' OR V_INC_I_POINTS > 5 OR V_INC_I_POINTS < 5 THEN
    RAISE E_B;
    END IF;

或者

为了防止人们在 PP_ID 为“PP01”的情况下输入大于 1 且小于 1 的值。

    IF V_PP_ID = 'PP01' OR V_INC_I_POINTS > 1 OR V_INC_I_POINTS < 1 THEN
    RAISE E_C;
    END IF;

它不起作用,只遵循第一个。我该如何克服呢?提前致谢。非常感谢您的帮助。

4

1 回答 1

1

我认为您的情况不正确:

IF V_PP_ID = 'PP03' OR V_INC_I_POINTS > 10 OR V_INC_I_POINTS < 10 THEN只要V_PP_ID等于 'PP03'(无论 V_INC_I_POINTS 的值如何)和V_INC_I_POINTS不等于 10(无论 V_PP_ID 的值如何),都将评估为真。

我相信预期的条件是:

IF V_PP_ID = 'PP03' AND (V_INC_I_POINTS > 10 OR V_INC_I_POINTS < 10) THEN

或者简单地说:

IF V_PP_ID = 'PP03' AND V_INC_I_POINTS != 10 THEN

(关于“PP02”和“PP01”的其余条件应相应修改。)

编辑:

我第一次错过了它,但似乎“V_PP_ID”从未设置为任何值。我们需要将其实例化为所选行的PP_ID列的值。

尝试更换:

SELECT I_POINTS INTO v_I_POINTS 
    FROM incident WHERE icd_id = v_icd_id;

SELECT I_POINTS, PP_ID INTO v_I_POINTS, V_PP_ID 
    FROM incident WHERE icd_id = v_icd_id;
于 2013-05-04T12:42:49.313 回答