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

oracle存储过程学习(-)-基本过程

2014-04-03 11:58 369 查看
1.存储过程语法

create or replace procedure 存储过程名

is

begin

  

null;

end;

2.新建一个存储过程

create or replace procedure SELECT_PACT_TYPE
is
--定义变量
pactTypeId number;
typeName nvarchar2(100);
begin
select t.pact_type_id,t.pact_type_name into pactTypeId, typeName from crm_pact_type t where t.pact_type_id=2;--select into 赋值给变量 from
dbms_output.put_line(pactTypeId || typeName);--调试输出
exception
when no_data_found then null;--未查到数据
WHEN OTHERS THEN RAISE;
end ;

3.如果查询多条数据,用游标,把上面的改进

create or replace procedure SELECT_PACT_TYPE
is
--定义变量
pactTypeId number;
typeName nvarchar2(100);
--游标
cursor c is select t.pact_type_id,t.pact_type_name  from crm_pact_type t ;
begin
--循环游标
for cr in c loop
begin
--赋值
pactTypeId := cr.pact_type_id;
typeName := cr.pact_type_name;
dbms_output.put_line( pactTypeId || typeName );
end;
end loop;
exception
when no_data_found then null;
end ;

4.带参数的查询

create or replace procedure SELECT_PACT_TYPE(id in number)
is
--定义变量
pactTypeId number;
typeName nvarchar2(100);
--游标
cursor c is select t.pact_type_id,t.pact_type_name  from crm_pact_type t where t.pact_type_id=id;
begin
--循环游标
for cr in c loop
begin
--赋值
pactTypeId := cr.pact_type_id;
typeName := cr.pact_type_name;
dbms_output.put_line( pactTypeId || typeName );
end;
end loop;
exception
when no_data_found then null;
end ;


5.调用上面带参数存储过程

declare
i number;
begin
i:=2;
-- 调用 id=>i id是存储过程的形参 ,i 是实际参数的值 ,将i的值赋给了id
select_pact_type(id => i);
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oracle plsql