存储过程、存储函数

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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值