declare totalscore tablec.score%type; begin for cur in (select * from tablec) loop totalscore := totalscore + cur.score; end loop; end;
DECLARE num NUMBER:=0; BEGIN FOR i IN 1..100 LOOP num:=num+i; END LOOP; Dbms_Output.put_line('num='||num); END; PL/SQL block, executed in 0 sec. num=5050 Total execution time 0 sec.
-----------Y的,奇怪了,怎么没值呢? SQL> select * from tablec;STUDENT SCORE -------------------------------------------------------------------------------- ---------- yjl49 100 sss 120SQL> SQL> declare 2 totalscore tablec.score%type; 3 begin 4 for cur in (select * from tablec) loop 5 totalscore:= totalscore + cur.score; 6 end loop; 7 dbms_output.put_line('totalscore'||totalscore); 8 end; 9 /totalscorePL/SQL procedure successfully completedSQL>
游标取的值都是一样的 且都是数字的? declare n number; sn number:=0; cursor cur is select id from tb; begin open cur; loop fetch cur into n sn:=sn+n; exit when cur%notfound; end loop; close cur; end;
declare n number; sn number:=0; cursor cur is select id from tb; begin open cur; loop fetch cur into n; sn:=sn+n; exit when cur%notfound; end loop; close cur; end;
totalmoney,可是结果是最后一次的值?for allbankMoneyTmp in allbankMoney loopbegin
totalmoney:=+allbankMoneyTmp.money;end ;end loop ;
declare
totalscore tablec.score%type;
begin
for cur in (select * from tablec) loop
totalscore := totalscore + cur.score;
end loop;
end;
DECLARE
num NUMBER:=0;
BEGIN
FOR i IN 1..100 LOOP
num:=num+i;
END LOOP;
Dbms_Output.put_line('num='||num);
END; PL/SQL block, executed in 0 sec.
num=5050
Total execution time 0 sec.
SQL> select * from tablec;STUDENT SCORE
-------------------------------------------------------------------------------- ----------
yjl49 100
sss 120SQL>
SQL> declare
2 totalscore tablec.score%type;
3 begin
4 for cur in (select * from tablec) loop
5 totalscore:= totalscore + cur.score;
6 end loop;
7 dbms_output.put_line('totalscore'||totalscore);
8 end;
9 /totalscorePL/SQL procedure successfully completedSQL>
declare
n number;
sn number:=0;
cursor cur is select id from tb;
begin
open cur;
loop
fetch cur into n
sn:=sn+n;
exit when cur%notfound;
end loop;
close cur;
end;
n number;
sn number:=0;
cursor cur is select id from tb;
begin
open cur;
loop
fetch cur into n;
sn:=sn+n;
exit when cur%notfound;
end loop;
close cur;
end;