為什么初始值是0,然后每個+1后輸出的結果成這樣的呢
set serveroutput on
delcare
begin
cursor ?cemp ? is select to char(hiredate,'yyyy') from ?emp;
pdiredate vachar2(4);
count80 ?number=0;
count81 ?number=0;
count82 ?number=0;
打開光標
open ?emp;
循環
loop
fetch ?cemp ?into phiredate;
exit ?when cemp%notfound;
判斷如實年份
if phiredate='1980' then count80=count80+1;
elsif phiredate='1981' then count81=count81+1;
elsif phiredate='1982' then count82=count82+1;
else phiredate='1987' then count87=count87+1;
end if;
end loop;
關閉關閉
close ?emp;
dbms_output.put_line("Total":||(count80+count81+count82+count87));
dbms_output.put_line(1980:||count80);
dbms_output.put_line(1981:||count81);
dbms_output.put_line(1982:||count82);
dbms_output.put_line(1987:||count87);
end;
2016-03-21
計數器只初始化一次,光標是一個集合。在loop循環里邊,每次循環取出一條記錄,比如第一次循環,取出的記錄是1980年的,那么count1就+1(由于count1初始值為0,所以此時count1的值1),然后就執行下一次循環(執行了if就不執行elsif和else了),第二次循環如果取出的還是1980年的,那count1就+1(此時count1的值為2).其它的計數器就不加。以此類推,一共循環14次。循環結束后,四個計數器都有值了