Oracle (5) 存储过程

本文介绍了Oracle存储过程的基本概念和使用,将其比喻为函数,并提供了创建、调用存储过程的通用结构和示例。强调了存储过程的安全性,特别是在代码执行中提交的重要性,以确保数据库状态的稳定性。

存储过程

我感觉它就是一个函数呀....大概没get到点,求告知。

【通用结构】

create or replace procedure 存储名 (输入变量名 变量的数据结构 )

as

begin

     操作语句

end;

【通用语句】调用存储过程

exec 过程名()

【例题1】创建存储过程,删除score表中某学号的学生数据

create or replace procedure p_delscore
(v_sno score.sno%type)
as
begin
  delete from score where sno=v_sno;
  if sql%rowcount=0 then
    dbms_output.put_line('nodata');
    else
    dbms_output.put_line('delete'||sql%rowcount||'row');
    commit; 
  end if;
end;

【执行】exec p_delscore('0601020212')

【解析】数据操作成功,一定要写commit提交。存储过程最重要的就是安全性,如果代码运行停止了,防止数据库会处于某种不安全状态 。

 

【例题2】创建存储过程,给出需要修改的院名,修改sdept表中该院系的名称

create or replace procedure p_update
(v_name sdept.deptno%type,v_dname sdept.deptno%type)
as
begin
  update sdept set dname=v_dname where dname=v_name;
  if sql%rowcount!=0 then
    dbms_output.put_line('更新'||sql%rowcount||'行');
    commit;
    else
    dbms_output.put_line('没有数据更新');
  end if;
end;

【执行】exec p_update('计算机信息院','计算机学院')

 

【例题3】结合游标,创建存储过程,查找某一门课程的所有不及格学生的学号,将其储存在不及格表中。

create table failgrade(
  sno char(15),
  cno char(15),
  grade number(6,2)
)

create or replace procedure p_inscore
(v_cname course.cname%type)
as
  cursor scfail_cur is 
  select * from score where grade<60  and cno =(select cno from course where cname =v_cname);
    sf scfail_cur%rowtype;
begin
open scfail_cur;
loop
  fetch scfail_cur into sf;
  if scfail_cur%rowcount=0 then
  dbms_output.put_line('nodata');
    exit;
  else
    exit when scfail_cur%notfound; 
    insert into failgrade values(sf.sno,sf.cno,sf.grade);
  end if;
end loop;
close scfail_cur;
end;

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值