Oracle 10g存储过程学习一

--1、创建存储过程(无参数)

create or replace procedure out_time
is
begin
dbms_output.put_line(systimestamp);
end;
--调用存储过程
exec out_time;
call out_time();


  • --结果
    24-11月-10 08.59.35.500000000 上午 +08:00



--2、创建存储过程(有参数,且显示指定为输入参数,如果不指定参数模式,默认为输入参数)

create or replace procedure update_comtype_name
(param_name in communitytype.name%type)
is
begin
--修改名称
update communitytype
set communitytype.english_name = param_name
where communitytype.community_type_id = 'ebook';
commit;
--异常处理
exception
when no_data_found then
dbms_output.put_line('没有找到资源库');
end;

--调用存储过程
call update_comtype_name('电子图书');

--结果
--调用前

  • --3 ebook 电子图书 电子图书

--调用后

  • --3 ebook 电子图书 电子图书1


--3、创建存储过程(有参数,且显示指定为输出参数,同时赋默认值)

create or replace procedure update_comtype_name
(param_id in communitytype.community_type_id%type,new_name out communitytype.name%type)
is
begin
--查询修改后的值
select communitytype.english_name into new_name
from communitytype
where communitytype.community_type_id = param_id;
--输出
dbms_output.put_line(new_name);
end;
--调用存储过程(在pl/sql中打开commond窗口)
var new_name varchar2(500);
exec update_comtype_name('ebook',:new_name);

--结果

  • --PL/SQL procedure successfully completed
    --new_name
    ---------
    --电子图书



--4、修改后再查询修改后的值

create or replace procedure update_comtype_name
(
param_id in communitytype.community_type_id%type,
param_eng_name in communitytype.english_name%type,
new_name out communitytype.name%type
)
is
begin
--修改名称
update communitytype
set communitytype.english_name = param_eng_name
where communitytype.community_type_id = param_id;
commit;
--查询修改后的值
select communitytype.english_name into new_name
from communitytype
where communitytype.community_type_id = param_id;
--输出
dbms_output.put_line(new_name);
end;

--调用存储过程(在pl/sql中打开commond窗口)
var new_name varchar2(500);
exec update_comtype_name('ebook','电子图书修改',:new_name);


  • --结果
    --SQL> var new_name varchar2(500);
    --SQL> exec update_comtype_name('ebook','电子图书修改',:new_name);

    --PL/SQL procedure successfully completed
    --new_name
    -----------
    --电子图书修改



--5、删除过程

drop procedure update_comtype_name;


--6、创建存储过程时,将参数同时设置为输入和输出

create or replace procedure compute
(num1 in out number,num2 in out number)
is
--在利用输入参数时,需要定义临时变量将运算结果保存起来
v1 number;
v2 number;
begin
v1:=num1/num2;
v2:=mod(num1,num2);
--最后返回结果时,将临时参数的值赋给输入参数(即输出参数)
num1:=v1;
num2:=v2;
end;


  • SQL> var n1 number;
    SQL> var n2 number;
    SQL> exec :n1 :=100

    PL/SQL procedure successfully completed
    n1
    ---------
    100

    SQL> exec :n2:=30

    PL/SQL procedure successfully completed
    n2
    ---------
    30

    SQL> exec compute(:n1,:n2);

    PL/SQL procedure successfully completed
    n1
    ---------
    3.33333333333333
    n2
    ---------
    10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值