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

来源:互联网 发布:网络安全法专家论证会 编辑:程序博客网 时间:2024/05/16 12:18

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;



0 0
原创粉丝点击