如何在PL / sql游标中获取用户输入LOOP并根据用户输入更新表?

时间:2018-03-28 07:35:49

标签: oracle plsql

我正在使用与emp表相同的emp3表。

DECLARE 
  CURSOR incr_cur IS SELECT * FROM emp3 FOR UPDATE OF sal;
   v_job emp3.job%TYPE := '&ENTER_Job';
   v_cnt INTEGER;
BEGIN
 FOR r_l IN incr_cur LOOP
  IF v_job = r_l.job THEN
   UPDATE emp3 SET sal = sal + 100 WHERE CURRENT OF incr_cur;
  END IF;
 END LOOP;

FOR p_l IN incr_cur LOOP
IF v_job = p_l.job THEN
  DBMS_OUTPUT.PUT_LINE('The Salary of ' || p_l.ename || ' is: ' || p_l.sal || ' (Incremented).');
ELSE
  DBMS_OUTPUT.PUT_LINE('The Salary of ' || p_l.ename || ' is: ' || p_l.sal || ' (Not Incremented).');
END IF;
END LOOP;
END;

执行脚本后,它会询问用户输入。 我给了INPUT'CLERK'和OUTPUT, Output

但我想要这样的输出, OutPUT1

2 个答案:

答案 0 :(得分:1)

只需在if条件后递增一个变量,然后在结尾显示它。

DECLARE
    CURSOR ..
    ..
    v_cnt INTEGER;

    BEGIN
    ..
    ..
      FOR p_l in incr_cur  --second for loop
       LOOP
         IF v_job = p_l.job
          v_cnt := v_cnt + 1;
      ..
      END LOOP;
    DBMS_OUTPUT.PUT_LINE('The Salary of '||v_cnt||' Employees are Incremented by 100');
END;

答案 1 :(得分:0)

DECLARE 
CURSOR incr_cur IS SELECT * FROM emp3 FOR UPDATE OF sal;
v_job emp3.job%TYPE := '&ENTER_Job';
v_cnt INTEGER := 0;
BEGIN
FOR r_l IN incr_cur LOOP
  IF v_job = r_l.job THEN
    UPDATE emp3 SET sal = sal + 100 WHERE CURRENT OF incr_cur;
  END IF;
END LOOP;

FOR p_l IN incr_cur LOOP
  IF v_job = p_l.job THEN
    v_cnt := v_cnt + 1;
    DBMS_OUTPUT.PUT_LINE('The Salary of ' || p_l.ename || ' is: ' || p_l.sal || ' (Incremented).');
  ELSE
    DBMS_OUTPUT.PUT_LINE('The Salary of ' || p_l.ename || ' is: ' || p_l.sal || ' (Not Incremented).');
  END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('The Salary of '|| v_cnt ||' Employees are Incremented by 100');
END;