if-else运用
declare
v_num number(8);
begin
v_num :=&n;
if v_num>0 and v_num<100 then
dbms_output.put_line('工薪阶级');
elsif v_num between 100 and 200 then
dbms_output.put_line('中产阶级');
else
dbms_output.put_line('资本家');
end if;
end;
case运用
declare
v_num number(8) :=&n;
v_result varchar2(15);
begin
case
when v_num =1 then v_result :='剪刀';
when v_num =2 then v_result :='石头';
when v_num =3 then v_result :='布';
else
v_result :='输入的数字无效';
end case;
dbms_output.put_line(v_result);
end;
--案例:1-10输出(loop循环实现)
declare
i number(8) :=1;
begin
loop
dbms_output.put_line(i);
exit when i =10;
--改变初始值
i := i+1;
end loop;
end;
/
--while循环实现上面例子
declare
i number(8) :=1;
begin
while i<=10 loop
dbms_output.put_line(i);
--改变初始值
i := i+1;
end loop;
end;
/
--for循环输出1-10
结构特点
for i in() loop
end loop;
declare
--i number(8) :=&n;
begin
for i in 1..10 loop --初始值..结束值
dbms_output.put_line();
end loop;
end;
/
for循环反向输出 10-1
declare
--i number(8) :=&n;
begin
for i in resever 1..10 loop --初始值..结束值
dbms_output.put_line(i);
end loop;
end;
/
declare
v_a number(8);
begin
v_a :=1;
if v_a= 1 then
dbms_output.put
declare
num number(8) :=&n;
result number(8);
--f(n) number(8);
begin
case
when num=1 then result :=1;
when num=2 then result =result
作业if-else 结构
1、输入员工的编号,判断此员工的工资:
-如果工资sal<1000则让工资在原来在基础上加上100
-如果工资1000<= sal <2000则让工资在原来在基础上加上200
-否则让员工工资在原来的基础上加上300;
declare
v_empno number(30) :=&n;
v_sal number(30);
begin
select sal into v_sal from emp where empno=v_empno;
if v_sal<1000 then v_sal :=v_sal +100;
elsif v_sal between 1000 and 2000 then v_sal:=v_sal+200;
else
v_sal :=v_sal+300;
end if;
dbms_output.put_line(v_sal);
end;
/
2.编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该员工工资增加10%。(使用if)
declare
v_ename varchar2(30) :=&n;
v_sal number(30);
begin
select sal into v_sal from emp where ename=v_ename;
if v_sal<2000 then v_sal :=v_sal+(v_sal*0.1);
end if;
dbms_output.put_line(v_sal);
end;
/
3.编写一个过程,可以输入一个雇员名,如果该雇员的补助不是0 就在原来的基础上增加100;如果补助为0 就把补助设为200;(使用if...else)
declare
v_ename varchar2(20) :=&n;
--v_money number(30);
v_nvl number(20);
begin
select nvl(comm,0) into v_nvl from emp where ename=v_ename;
if(v_nvl =0) then v_nvl :=v_nvl+200;
else v_nvl := v_nvl+100;
end if;
dbms_output.put_line(v_nvl);
end;
/
--4、编写一个过程,可以输入一个雇员编号,如果该雇员的职位是PRESIDENT就给他的工资增加1000,
--如果该雇员的职位是MANAGER 就给他的工资增加500,其它职位的雇员工资增加200。(使用if..elsif..else)
declare
v_empno number(32) :=&n;
v_job varchar2(20);
v_sal number(20);
begin
select job,sal into v_job,v_sal from emp where empno=v_empno;
if(v_job ='PRESIDENT') then v_sal :=v_sal+1000;
elsif (v_job = 'MANAGER') then v_sal :=v_sal+500;
else v_sal :=v_sal+200;
end if;
dbms_output.put_line(v_sal);
end;
/
作业循环结构
1、循环输出“haha1...haha10”(使用while)
declare
i number(5) :=1;
begin
while i<10 loop
dbms_output.put_line('haha' || i);
i :=i+1;
end loop;
end;
/
2、把上述示例改为loop实现
declare
i number(5) :=1;
begin
loop
dbms_output.put_line('haha' || i);
exit when i =10;
i :=i+1;
end loop;
end;
/
3、现有一张表users,字段(uid,uname),分别使用(loop、while、for完成)。
请编写一个过程,可以输入用户名,并循环添加10 个用户到users 表中,用户编号从1 开始增加。
create table users
(
userid number(8),
uname varchar2(20)
);
declare
v_id number(8):=1;
v_name varchar2(20);
begin
while v_id<=3 loop
v_name:='&name';
insert into users values(v_id,v_name);
v_id:=v_id+1;
end loop;
end;
/
4、打印九九乘法表
这个还要修改
declare
v_result number(20);
--i number(20) :=1;
begin
for i in 1..9 loop;
for j in 1..9 loop;
v_result :=i*j;
if length(i*j)=1 and j!=1 then
dbms_output.put_line(' ');
end if;
dbms_output.put_line(i || '*'||j ||'='||v_result);
dbms_output.put_line(' ');
end loop;
end;
这是对的
declare
begin
for i in 1..9 loop
for j in 1..i loop
dbms_output.put(i);
dbms_output.put('*');
dbms_output.put(j);
dbms_output.put('=');
if length(i*j)=1 and j!=1 then
dbms_output.put(' ');
end if;
dbms_output.put(i*j);
dbms_output.put(' ');
end loop;
dbms_output.put_line(' ');
end loop;
end;