oracle 存储过程基本过程一

1.看一个范例:

create or replace procedure firstPro( param1 in varchar,param2 in integer)
is
--定义变量
       var1 int ;
       var2 varchar(20) ;
       sqllog varchar(20);
       vs_msg varchar(4000);
       pro_names varchar(50);
       log_info_count integer;
  --定义游标
  cursor cur_1 is
  select * from hive.tets_pro_1
  where 1=1
  group by names ,age
  ;
begin
--变量初始化
       var2 := param1;  
       pro_names :='firstPro';
--1.删除表中特定条件的数据
delete from hive.tets_pro where age=param1;
--1.用系统内置函数查看受影响的行数
DBMS_OUTPUT.put_line('delete年龄等于'||param1||'的记录'||SQL%rowcount||'条');
--1.删除操作完成
--2.For 语句遍历游标
For rec in cur_1 loop
update hive.tets_pro
set age=rec.age
where names = rec.names;
end loop;
commit;
--2.用系统内置函数查看受影响的行数
DBMS_OUTPUT.put_line('游标返回结果'||SQL%rowcount||'条(update不会返回结果的,亲)');
--2.For 语句遍历游标操作完成
/*Select count(*) into var1 from hive.tets_pro where AGE= param1 ;
       If (var1 = 1) then
          --Select 列名 into 变量2 from 表A where列名=param1;
          Dbms_output.Put_line('记录数为1条');
       Elsif (var1 > 1) then
          Dbms_output.Put_line('记录数大于1条');
       Else
          --RAISE_APPLICATION_ERROR(-20123,'Invald product code' TRUE);
          Dbms_output.Put_line('记录数0条');
       End if;
*/  
--3.创建日志表  在同一个存储过程创建表是不可以的,下面将错误插入表的时候会报错,这部分应该提前建错误日志表。
    select count(*) into log_info_count from user_tables where table_name=upper('log_info');
    if  log_info_count = 0 then 
    execute immediate 'create table hive.log_info(
                        proc_name varchar(50),
                        error_sqlcode varchar(50),
                        error_info varchar(3000),
                        opdate date)';
    end if;
Exception
    When too_many_rows then
    Dbms_output.Put_line('记录数多余1条');
--3.将错误日志写进日志表
    when others then
    vs_msg :=SUBSTR(SQLERRM,1,200);
    Rollback;
--3.将错误信息插入日志表
    DBMS_OUTPUT.put_line('请查看错误日志:hive.log_info,存储过程名称:'||pro_names);
    insert into hive.log_info(proc_name,error_sqlcode,error_info,opdate)
    values('pro_names','',vs_msg,SYSDATE);
    commit;
    return;
--3.将错误信息插入日志表        
    --DBMS_OUTPUT.put_line('sqlcode : ' ||sqlcode); 
    --DBMS_OUTPUT.put_line('sqlerrm : ' ||sqlerrm); 
end;

2. 存储过程 赋值的2种方式

    1)  pro_names :='firstPro';  

    2) select count(*) into log_info_count from user_tables where table_name=upper('log_info');




猜你喜欢

转载自blog.csdn.net/beishafengjiang/article/details/80856363