1.创建存储过程
(1) 将下列的未命名的PL/SQL,转换成存储过程,存储过程名自己设定,注意比较未命名的 PL/SQL与命名的 PL/SQL 的差别,如没有where current of是什么情况。
declare
cursor emp_cursor is select * from emp where deptno=10 for update;
begin
for emp_record in emp_cursor loop
dbms_output.put_line(emp_record.sal);
update emp set sal=sal*1.1 where current of emp_cursor;
end loop;
end;
/
如果没有红线部分,则所有的sal都会改变。
(2) 创建存储过程“dept_count_pro”,通过传入参数部门号deptno(如10),显示员工表“emp”中不同部门的员工人数,并执行该存储过程。
CREATE OR REPLACE PROCEDURE TONGJI (userid IN NUMBER)
AS
TEMP NUMBER;
BEGIN
select count(*) into TEMP from emp where deptno=userid;
dbms_output.put_line(TEMP);
end;
/
(3).查看存储过程
利用 SQL*Plus或 iSQL*Plus 从 user_source数据字典中查看存储过程。
Select text from user_source where name='TONGJI';
(4).删除存储过程
(1) 利用 SQL*Plus或 iSQL*Plus删除某个存储过程。
Drop procedure TONGJI
2.创建函数
(1) 创建存储函数“emp_fun”,通过传入参数员工的编号,根据传入的员工编号,检查该员工是否存在。如果存在,则返回员工的姓名,否则返回“此员工不存在“,并执行该存储函数。
create or replace function get_name(emp_num number) return varchar2
as
emp_name emp.ename%type;
begin
select ename into emp_name from emp where empno=emp_num;
return emp_name;
exception
when NO_DATA_FOUNE then
dbms_output.put_line('用户名为空');
end;
/
(2) 创建存储函数“dept_count_fun”,利用传入参数传入部门号 (如10),返回员工表“emp”中不同部门的员工人数,并执行该存储函数,注意比较与存储过程“dept_count_pro”的差别。
create or replace function TONGJI (userid number) return number
as
temp emp.deptno%type;
begin
select count(*) into temp from emp where deptno=userid;
return temp;
end;
/
(3)查看存储函数
从 user_source 数据字典中查看存储函数。
Select text from user_source where name='TONGJI';
(4)删除存储函数
(1) 删除存储函数“dept_count_fun”。
drop function TONGJI;