您的位置:首页 > 数据库 > Oracle

Oracle 10g存储过程学习一

2014-10-30 11:31 330 查看
--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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: