pl/sql练习:oracle14(跟着宝哥学java:oracle系列:全网最全):pl/sql练习、循环、for、while、loop
·
--1 判断输入的数是不是水仙花数
declare
n int:='&请输入一个数:';
a int;
b int;
c int;
begin
if n <100 or n >=1000 then
dbms_output.put_line(n||'不是三位数,肯定就不是水仙花数!');
return;
end if;
-- 获取给个位数的值
a:=mod(n,10);
b:=mod(trunc(n/10,0),10);
c:=trunc(n/100,0);
-- 判断各个位数的三次方之和 是否等于次数
if a*a*a+b*b*b+c*c*c=n then
dbms_output.put_line(n||'是水仙花数!');
else
dbms_output.put_line(n||'是三位数,但不是水仙花数!');
end if;
exception
when others then
dbms_output.put_line('出错了!!!:::::');
end;

--2 输入年月日 求此时间距离今天的天数
declare
y int:='&请输入一个年:';
m int:='&请输入一个月:';
d int:='&请输入一个日:';
days int;
date1 date;
begin
date1:=to_date(y||'-'||m||'-'||d,'yyyy-mm-dd');
days:=sysdate-date1;
dbms_output.put_line(date1||'距离今天有'||days||'天!');
exception
when others then
dbms_output.put_line('出错了!!!:::::');
end;

-- 2.1 判断一个年 是不是闰年
declare
y int:='&请输入一个年:';
begin
if mod(y,400) = 0 then
dbms_output.put_line(y||'是闰年!');
elsif mod(y,4) = 0 and mod(y,100) != 0 then
dbms_output.put_line(y||'是闰年!');
else
dbms_output.put_line(y||'不是闰年!');
end if;
exception
when others then
dbms_output.put_line('出错了!!!:::::');
end;
-- 条件合并
declare
y int:='&请输入一个年:';
begin
if mod(y,400) = 0 or (mod(y,4) = 0 and mod(y,100) != 0) then
dbms_output.put_line(y||'是闰年!');
else
dbms_output.put_line(y||'不是闰年!');
end if;
exception
when others then
dbms_output.put_line('出错了!!!:::::');
end;

-- 2.2 判断指定年 指定月 有多少天
declare
y int:='&请输入一个年:';
m int:='&请输入一个月:';
days int;
begin
case m
when 4 then
days:=30;
when 6 then
days:=30;
when 9 then
days:=30;
when 11 then
days:=30;
when 2 then
if mod(y,400) = 0 or (mod(y,4) = 0 and mod(y,100) != 0) then
days:=29;
else
days:=28;
end if;
else
days:=31;
end case;
dbms_output.put_line(y||'年'||m||'月有'||days||'天!');
exception
when others then
dbms_output.put_line('出错了!!!:::::');
end;

--3 查询student表某个学生的年龄和性别 判断称呼:8个称呼
alter table stuinfo add sex char(3);
update stuinfo set sex='女';
update stuinfo set sex='男' where mod(sno,2)=0;
select * from stuinfo;
declare
sn int:='&请输入一个学号:';
sx char(3);
sg int;
changHu varchar(20);
begin
select sex,sage into sx,sg from stuinfo where sno=sn;
if sx = '男' then
if sg < 18 then
changHu:='小男孩';
elsif sg < 35 then
changHu:='帅哥';
elsif sg < 60 then
changHu:='叔叔';
else
changHu:='爷爷';
end if;
else
if sg < 18 then
changHu:='小女孩';
elsif sg < 35 then
changHu:='美女';
elsif sg < 60 then
changHu:='阿姨';
else
changHu:='奶奶';
end if;
end if;
dbms_output.put_line(sx||':'||sg||':::称呼是:'||changHu);
exception
when others then
dbms_output.put_line('出错了!!!:::::');
end;

/*
循环结构:loop while for
循环结构之loop: loop格式
loop
循环执行的代码;
if 条件 then
exit;
end if;
end loop;
*/
-- 打印1到100的和
declare
sumn int:=0;
n int:=1;
begin
loop
sumn:=sumn+n;
n:=n+1;
if n >100 then
exit;
end if;
end loop;
dbms_output.put_line('1到100的和:'||sumn);
exception
when others then
dbms_output.put_line('出错了!!!:::::');
end;

-- 99乘法表
declare
i int:=1;
j int:=1;
begin
loop
j:=1;
loop
dbms_output.put(i||'*'||j||'='||i*j||' ');
j:=j+1;
if j > i then
exit;
end if;
end loop;
i:=i+1;
if i > 9 then
exit;
end if;
dbms_output.put_line('');
end loop;
exception
when others then
dbms_output.put_line('出错了!!!:::::');
end;

/*
循环结构之while: while格式
while 条件 loop
循环执行的代码;
end loop;
*/
-- 99乘法表
declare
i int:=1;
j int:=1;
begin
while i<=9 loop
j:=1;
while j<=i loop
dbms_output.put(i||'*'||j||'='||i*j||' ');
j:=j+1;
end loop;
i:=i+1;
dbms_output.put_line('');
end loop;
exception
when others then
dbms_output.put_line('出错了!!!:::::');
end;
-- 判断一个数是不是质数
declare
n int:='&请输入一个数:';
m int:=2;
b boolean:=true;
begin
while m < n loop
if mod(n,m)=0 then
b:=false;
exit;
end if;
m:=m+1;
end loop;
if b=true and n >1 then
dbms_output.put_line(n||'是质数!');
end if;
exception
when others then
dbms_output.put_line('出错了!!!:::::');
end;
/*
循环结构之for: for格式
for n in start..end loop
循环执行的代码;
end loop;
*/
-- 获取1到100的和
declare
sumn int:=0;
begin
for n in 1..100 loop
sumn:=sumn+n;
end loop;
dbms_output.put_line('1到100的和:'||sumn);
exception
when others then
dbms_output.put_line('出错了!!!:::::');
end;
-- 99乘法表
begin
for i in 1..9 loop
for j in 1..i loop
dbms_output.put(i||'*'||j||'='||i*j||' ');
end loop;
dbms_output.put_line('');
end loop;
exception
when others then
dbms_output.put_line('出错了!!!:::::');
end;
-- 获取一个字符串 数字字符 字母字符 和其他字符的次数
declare
str varchar(100):='&请输入一个字符串:';
sz int:=0;
zm int:=0;
qt int:=0;
c char(3);
begin
for n in 1..length(str) loop
-- dbms_output.put_line(n||'::'||substr(str,n,1));
-- 获取n位置处的字符
c:=substr(str,n,1);
if ascii(c)>=ascii('0') and ascii(c)<=ascii('9') then
sz:=sz+1;
elsif ascii(c)>=ascii('a') and ascii(c)<=ascii('z') then
zm:=zm+1;
elsif ascii(c)>=ascii('A') and ascii(c)<=ascii('Z') then
zm:=zm+1;
else
qt:=qt+1;
end if;
end loop;
dbms_output.put_line(str||'中有'||sz||'个数字,'||zm||'个字母,'||qt||'个其他字符!');
exception
when others then
dbms_output.put_line('出错了!!!:::::');
end;

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)