如何限制在存储过程中输入的数据?

时间:2013-05-04 11:43:27

标签: stored-procedures plsql

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

以下是表格:

       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;

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

防止人们输入的值大于5且小于5,其中PP_ID为“PP02”。

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

防止人们输入大于1且小于1的值,其中PP_ID为“PP01”。

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

它不起作用,只跟随第一个。我怎么能克服这个? 提前致谢。非常感谢帮助。

1 个答案:

答案 0 :(得分:1)

我认为你的情况不正确:

每当 V_PP_ID 等于'PP03'时(无论V_INC_I_POINTS的值如何),只要 V_INC_I_POINTS 不等于10(无论如何),

IF V_PP_ID = 'PP03' OR V_INC_I_POINTS > 10 OR V_INC_I_POINTS < 10 THEN将评估为真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;