存储过程
我感觉它就是一个函数呀....大概没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;
本文介绍了Oracle存储过程的基本概念和使用,将其比喻为函数,并提供了创建、调用存储过程的通用结构和示例。强调了存储过程的安全性,特别是在代码执行中提交的重要性,以确保数据库状态的稳定性。

1076

被折叠的 条评论
为什么被折叠?



