pl/sql编写一个块,可以输入部门号,并显示该部门所有员工姓名和他的工资。
declare
--定义游标类型sp_emp_cursor
type sp_emp_cursor is ref cursor;
--定义一个游标变量
test_cursor sp_emp_cursor;
--定义变量
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
--执行部分
--把test_cursor和一个select结合,这个游标指向这个select语句
--执行select得到一堆数据,此游标指向这个结果集,
open test_cursor for select ename,sal from emp where deptno=&no;
--循环取出
loop
--fetch就是取出
fetch test_cursor into v_ename,v_sal;
--判断是否test_cursor为空
exit when test_cursor%notfound;
dbms_output.put_line('姓名:'||v_ename||',工资:'||v_sal);
end loop;
end;
-------------------------期望目标if及loop及while及for
1:掌握pl/sql的高级用法(分页过程模块,订单过程模块)
2:会处理oracle常见的例外
3:会编写oracle各种触发器
4:理解视图的概念并能灵活使用视图
-----------------pl/sql的进阶 控制结构
A:条件分支语句
pl/sql中提供了三种条件分支语句
if-----then,if-----then----else, if----then-------elsif----else
a:if----then
?编写一个过程,可以输入一个雇员号,如果该雇员的工资低于2000,给该雇员工资增加10%
---开始---
create or replace procedure sp_proce(sp_Name varchar2) is
--定义部分
v_sal emp.sal%type;
begin
--执行部分
select sal into v_sal from emp where ename=sp_Name;
--判断
if v_sal<2000 then
update emp set sal=sal*1.1 where ename=sp_Name;
end if;
end;
b:二重条件分支 if---then--else
?编写一个过程,可以输入一个雇员名,如果该雇员的补助不是0就在原来的基础上增加100
如果补助为0就把补助设为200;
create or replace procedure sp_proce(sp_Name varchar2) is
--定义部分
v_comm emp.comm%type;
begin
--执行部分
select comm into v_comm from emp where ename=sp_Name;
--判断<>表示不等于
if v_comm<>0 then
update emp set comm=comm+100 where ename=sp_Name;
else
update emp set comm=comm+200 where ename=sp_Name;
end if;
end;
c:多重条件分支,if--then--elsif--else
?编写一个过程,可以输入一个雇员编号,如果该雇员的职位是PERSIDENT就给他的工资增加1000,如果
该雇员的职位是MANAGER就给他的工资增加500,其它职位的雇员工资增加200
create or replace procedure sp_proce1(spNo number) is
--定义
v_job emp.job%type;
begin
--执行
select job into v_job from emp where empno=spNo;
if v_job='PRESIDENT' then
update emp set sal=sal+1000 where empno=spNo;
elsif v_job='MANAGER' then
update emp set sal=sal+500 where empno=spNo;
else
update emp set sal=sal+200 where empno=spNo;
end if;
end;
-----基本循环至少要执行循环体一次,而对于while循环来说,只有条件为true时,才会执行循环体语句,
while循环以while...loop开始,以loop end结束
案例:现有一张表users表结构userId,userName
eg:请编写一个过程,可输入用户名,并循环添加10个用户到表中,用户编号 从11开始
create or replace procedure sp_proce3(spName varchar2) is
--定义
v_num number:=11;
begin
--执行
while v_num<=20 loop
insert into users values(v_num,spName);
v_num:=v_num+1;
end loop;
end;
-------------------- 我们不建议使用它,还是使用loop吧
declare
type sp_emp_cursor is ref cursor;
test_cursor sp_emp_cursor;
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
open test_cursor for select ename,sal from emp where deptno=&no;
loop
fetch test_cursor into v_ename,v_sal;
exit when test_cursor%notfound;
dbms_output.put_line('姓名:'||v_ename||',工资:'||v_sal);
end loop;
end;
-----------------------------顺序控制语句 goto,null
1:goto
goto语句用于跳转到特定标号去执行语句,注意由于使用goto语句会增加程序的复杂性,
并使得应用程序可读性变差,做一般应用开发时,建议大家不要使用goto语句。
eg:在一种情况下会使用到的,多重循环,从最里循环直接跳到最外层循环,
declare
i int:=1;
begin
loop
dbms_output.put_line('i='||i);
if i=10 then
goto end_loop;
end if;
i:=i+1;
end loop;
<<end_loop>>
dbms_output.put_line('循环结束');
end;
2:null
null语句不会执行任何操作,并且会直接将控制传递到下一条语句,使用null语句的主要好处是可以
提高pl/sql的可读性
declare
--定义部分
v_sal emp.sal%type;
v_ename emp.ename%type;
begin
--执行部分
select ename,sal into v_sal,v_ename from emp where empno=&no;
if v_sal <3000 then
update emp set comm=sal*0.1 where ename=v_ename;
else
null;
end else;
end;