---- 作业 输出 薪水等级 2 - 5 等级 最低和 最高薪水
set serveroutput on
declare mysal number:=1;
myhi number;
mylo number;
begin
loop
if(mysal>5) then
exit;
end if;
select losal,hisal into mylo,myhi from salgrade where grade=mysal;
dbms_output.put_line(mysal||'的最高薪水是'||myhi||'最低薪水是'||mylo);
mysal:=mysal+1;
end loop;
end;
--第二种方式
set serveroutput on --注意server的写法
declare mysal number:=2;
myhi number;
mylo number;
begin
loop
select losal,hisal into mylo,myhi from salgrade where grade=mysal;
dbms_output.put_line(mysal||'的最高薪水是'||myhi||'最低薪水是'||mylo);
mysal:=mysal+1;
exit when mysal=6;
end loop;
end;
--求出5的阶乘
set serveroutput on
declare a number:=5;
b number:=1;
begin
loop
b:=a*b;
a:=a-1;
exit when a=1;
end loop;
dbms_output.put_line(b); --注意变量的作用范围、
end;
-- 作业 plsql 编程 查询 emp 表 薪水大于3000
-- 有多个 就输出 有多个员工大于3000
-- 有1个 就输出 有1个员工大于3000
-- 没有 就输出 没有
set serveroutput on
declare a number;
begin
select count(*)into a from emp where sal>3000;
if(a>1)then
dbms_output.put_line('有一个员工大于3000');
elsif(a=0) then
--else if(a=0) then --注意用elsif结尾可以只用一个end if结束,否则,用多个
dbms_output.put_line('有零个员工大于3000');
else
dbms_output.put_line('有多个员工大于3000');
end if;
--end if;
end;
--case when 语句
set serveroutput on
declare a number:=1;
begin
case a --注意此时的用法
when 1 then
dbms_output.put_line('优秀');
when 2 then
dbms_output.put_line('良好');
else
dbms_output.put_line('及格');
end case;
end;
--case when 语句写法2
set serveroutput on
declare a number:=1;
b varchar2(32);
begin
b:= case a --注意此时的用法
when 1 then
'优秀' --没有分号
when 2 then
'良好'
else
'及格'
end case ;
dbms_output.put_line(b);
end;
plsql编程之,循环语句和判断语句
最新推荐文章于 2024-05-17 00:15:00 发布