PL/SQL程序基础2
一.实验目标
掌握较复杂的PL/SQL编程方法。具备如下能力:
- 针对数据库应用领域的数据需求,设计出基于Oracle数据库的解决方案的能力;
- 承担Oracle数据库系统的实施、运行与维护等基本工作的能力。
二.实验项目
- 在A_DB模式中有一个表tan1(a),表中有若干个整数(不要管是否有重复)。
请编写一个名为obj4_1 的存储函数(无参数),统计该表中有多少个数是素数,并作为函数返回值返回。
create or replace
function obj4_1 return integer
as
m integer:=0;
is_prim boolean;
begin
for e in (select * from A_DB.tan1) loop
begin
is_prim:=true;
for j in 2..trunc(sqrt(e.a)) loop
if mod(e.a,j)=0 then
begin
is_prim:=false;
exit;
end;
end if;
end loop;
if is_prim then
m:=m+1;
end if;
end;
end loop;
return m;
end;
.
/
select obj4_1() 素数个数 from DUAL;
-
在A_DB模式中有一个表tan3(a),表中有若干个整数(不要管是否有重复),假设这些数表示年份。请编写一个名为obj4_2的存储函数(无参数),统计该表中有多少个年份是闰年,并作为函数返回值返回。说明:闰年的计算方法:被400整除,或被4整除而不能被100整除的年份为闰年。说明:闰年的计算方法:被400整数,或被4整除而不能被100整除的年份为闰年。
create or replace
function obj4_2 return integer
as
total INTEGER:=0;
BEGIN
For y in (select distinct(A_DB.TAN3.A) from A_DB.TAN3)loop
if mod(y.A,400)=0 or (mod(y.A,4)=0 and mod(y.A,100)!=0) then
total:=total+1;
DBMS_OUTPUT.put_line(y.A);
End if;
End loop;
Return total;
End;
.
/<