oracle 存储过程
存储过程的使用
创建存储过程
create or replace procerdure 过程名
is
声明变量
begin
执行语句
exception
异常处理
end 过程名
查询存储过程
create or replace procedure mypp --创建存储过程
is
cursor mydd is select *from students; --定义游标,以为存储过程中不能写简单的select 语句,所以这里用游标进行查询
mtaa students%rowtype; --mtaa 类型是students表的所有列
begin
open mydd; --打开游标
loop
fetch mydd into mtaa; --循环移动游标指针便利游标,并将当前值赋给mtaa
exit when mydd%notfound; --当游标没有下一位的时候,退出循环
dbms_output.put_line(mtaa.stu_name); --输出查询结果中的stu_name
end loop;
end mypp; --存储过程结束,结尾的存储过程名可加可不加
begin
mypp;
end;
调用存储过程
begin
mypp; --存储过程名
end;
插入存储过程带输入参数
create or replace procedure myqq(
stuid students.stu_id%type,
stuname students.stu_name%type,
stutype students.stu_type%type, --定义参数,指明参数类型
studate students.stu_date%type
)
is
--申明变量,没有变量可不写
begin
insert into students values(stuid,stuname,stutype,studate); --将得到的参数插入表中
commit; --提交事物
end myqq;
begin
myqq('00004','黄蓉','00001',sysdate); --调用存储过程,并传参
end;
带输出和输入的存储过程,先判断是否存在,然后再插入
create or replace procedure myww(
stuid students.stu_id%type,
stuname students.stu_name%type,
stutype students.stu_type%type,
studate students.stu_date%type, --申明输入参数
re out number --声明输出参数,声明输出参数时 需在参数类型前加out
)
is
cursor mycc is select *from students where students.stu_id=stuid; --定义游标
mystu students%rowtype; --申明变量,并指明类型
begin
open mycc; --打开游标
fetch mycc into mystu; --移动游标指针,并同时将当前值赋值给 变量
if mycc%notfound then --如果游标没有下一个
re:=0; --给输出参数赋值
insert into students values(stuid,stuname,stutype,studate); --将数据插入表中
commit; --提交事物
else
re:=1; --否则 给输出参数赋值为1
end if;
close mycc; --关闭游标
end;
调用并传参
declare
re number; --定义变量,用来接受输出参数
begin
myww('00005','黄蓉','00001',sysdate,re); --调用存储过程 并传参
dbms_output.put_line(re);
end;
浙公网安备 33010602011771号