oracle遍一个过程,对1-100中能够被3整除的数进行求和,本人新手,求指教,谢谢

2024-12-21 19:22:23
推荐回答(3个)
回答1:

declare
i int:=1;
j int:=0;
begin
while i<=100
loop
if mod(i,3)=0
then
j:=j+i;
i:=i+1;
else
i:=i+1;
end if;
end loop;
Dbms_Output.Put_Line(j);
end;

回答2:

select sum(
case when mod(t.rnum,3) = 0 then
t.rnum
else 0 end
) as total_sum
from
(
SELECT rownum rnum
FROM ALL_OBJECTS
WHERE ROWNUM <= 100
) t
;

回答3:

create or replace procedure SumDataWhichCanDivideByThree as i_sumtotal number; i_ind number;begin for i in 1..100 loop if mod(i,3) =0 then i_sumtotal := i_sumtotal + i; end if; end loop;end;