Oracle的存储过程

CREATE OR REPLACE Procedure Prc_Demo Is
  --变量声明
  v_Index Number;--整型

  v_Date Date;--日期

  v_Varchar Varchar2(30); ---过程的参数,不能带长度,声明变量,可以带长度

  --    描定,即在不知表结构的时候经常使用。
  v_Var T2.t_Name%Type; --描定,%type列变量

  v_Row T2%Rowtype; --描定  %rowtype,行变量

  Type Ty_Demo Is Table Of T2%Rowtype Index By Binary_Integer; --定义一个数组类型

  v_Ty_Demo Ty_Demo; --声明一个数组变量

  --自定义异常
  myexception exception;

Begin
  v_Index := 0; --oracle中变量的赋值使用":="

   --注意:存储过程中不能包含纯粹的select语句,其必须是赋值语句。
  
  --    Into v_Index 给此字段v_Index赋值
  Select t.t_Root_Id Into v_Index From T2 t Where t.t_Id = 100100;
  
  Select To_Date('1994-09-09 15:32:54', 'yyyy-mm-dd hh24:mi:ss')
    Into v_Date
    From Dual;
  
  Select * Into v_Row From T2 t Where t.t_Id = 100100;
  
  Select t.t_Name Into v_Var From T2 t Where t.t_Id = 100100;
  Dbms_Output.Put_Line(v_Var);--    相当于system.out.println();

  -- 给数组赋值,bulk collect 意为将结果集先装入一个集合中,再将其装入数组中。
  select * bulk collect into v_ty_demo from t2 t;
  
  --    通过for循环某个字段的值
  for i in 1 .. v_ty_demo.count loop
    Dbms_Output.Put_Line(v_ty_demo(i).t_Name);
  end loop;

  
  v_Varchar := '张三';

  --    没有commit事务不会提交
  Insert Into T2
    (t_Id, t_Root_Id, t_Name, t_Description)
  Values
    (Seq_Test_Data.Nextval, 120, 'HY1H', 'abc');
  Commit;

  --    测试,如出现异常,事务回滚。
  Insert Into T2
    (t_Id, t_Root_Id, t_Name, t_Description)
  Values
    (Seq_Test_Data.Nextval,
     180,
     'nmnADSFASDFADSFASDFASDFASDFAFDASDF',
     '123');

  Commit;

  --   if语句
  If v_Index < 3 Then
    Dbms_Output.Put_Line('abc');
  Elsif v_Index >= 3 And v_Index < 10 Then
    Dbms_Output.Put_Line('bcd');
  Else
    Dbms_Output.Put_Line('xyz');
  End If;

  --    loop循环
  Loop
    If v_Index > 200 Then
      Exit;
    End If;
    Dbms_Output.Put_Line('123');
    v_Index := v_Index + 1;
  End Loop;


  if v_Index = 0 then 
     raise myexception; --    抛异常
   end if;

 --    输出异常信息
    Exception
  when myexception then 
    dbms_output.put_line('v_index等于0,非法数据');
    --    将错误信息进行记录    Prc_Error_Log('Prc_Demo','v_index等于0,非法数据',-20001);
  When Others Then
    Prc_Error_Log('Prc_Demo',sqlerrm,sqlcode);
End Prc_Demo;

 

CREATE OR REPLACE Procedure Prc_Error_Log(p_Process_Name In Varchar2,
                                          p_Error_Msg    In Varchar2,
                                          p_Error_Code   In Varchar2) Is
Begin

  Insert Into Sys_Error_Log
    (Log_Id, Log_Dt, Process_Name, Error_Msg, Error_Code)
  Values
    (Seq_Test_Data.Nextval, Sysdate, p_Process_Name, p_Error_Msg, p_Error_Code);
    
  commit;
End Prc_Error_Log;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值