-1
Declare
  type v_year_arr is varray(100) of emps%rowtype;
  v_deptid emps.department_id%type :=90;
  v_rem1 number(5,2);
  v_rem2 number(5,2);  
  v_rem3 number(5,2);
  v_year v_year_arr;
Begin
   select * bulk collect into v_year from emps where department_id = v_deptid;
   v_rem1 := MOD(v_year.yof,4);
   v_rem2 := MOD(v_year.yof,100);
   v_rem3 := MOD(v_year.yof,400);
   for i..v_year loop
      If (v_rem1=0 and v_rem2=0 and v_rem3=0)
      then
        DBMS_OUTPUT.PUT_LINE('Leap year :'||v_year.yof);
      else
        DBMS_OUTPUT.PUT_LINE('Not a Leap year :'||v_year.yof)
      End if;
   end loop;
end;
4

1 回答 1

2

我认为你最好使用游标 FOR 循环来编写它,如下所示:

Declare
  v_deptid emps.department_id%type :=90;
  v_rem1 number(5,2);
  v_rem2 number(5,2);  
  v_rem3 number(5,2);
Begin
  FOR aRow IN (select * from emps where department_id = v_deptid)
  LOOP
    v_rem1 := MOD(aRow.yof,4);
    v_rem2 := MOD(aRow.yof,100);
    v_rem3 := MOD(aRow.yof,400);

    If (v_rem1=0 and v_rem2<>0) or (v_rem2=0 and v_rem3=0) then
      DBMS_OUTPUT.PUT_LINE('Leap year :'||aRow.yof);
    else
      DBMS_OUTPUT.PUT_LINE('Not a Leap year :'||aRow.yof)
    End if;
  end loop;
end;

SELECT...BULK COLLECT INTO...这消除了语句返回超过 100 行的潜在问题。我还冒昧地纠正了你的闰年决定。

分享和享受。

于 2013-08-08T13:10:20.173 回答