oracle PL/SQL

SQL:没有编译

PL/SQL:已经编译过

PL/SQL的优点:

     1,提高程序是运行性能

     2, 提高模块化的程序设计功能

       可以先将企业规则或商业逻辑集成到PL/SQL 子程序中(过程,函数,包) 在应用程序中可以直接调用子程序,后期修改的也只需修改子程序,不必修改客户端的应用程序代码 

     3,允许定义标示符(变量,长量,游标,和异常等)

     4,具有过程语言控制结构

     5, 具备良好的兼容性(内置程序包)

     6, 处理运行错误(预先定义了常现的异常处理)

PL/SQL :只能直接嵌入select 语句,DML 语句和事务控制语句

PL/SQL 块包括了:无名块,匿名块,有名块(存储过程,函数)

pL/SQL 结构:

 declare 

    /*  定义 变量,常量,游标,裂解*/

begin

  /* sql语句    */

   exception

 /*  异常处理部分*/

end;
View Code

end 之后必须加分号

PL/SQL控制结构

  条件分支语句:  if

if   condition (判断的条件) then

   statements(sql语句);
 
[elsif  condition    then
    
    statements ;]

[ else
      statements ;]

     end if;
View Code

case 语句: 

case 条件选择符 
   when 指定条件的表达式  then 执行的条件操作;
     when 指定条件的表达式  then 执行的条件操作;
     [else 执行的条件操作 ] 
     end case;
View Code

循环语句: 

 --基本循环
     loop
       statement1  --执行语句
       exit [when condition1]  --退出条件
       end loop;
       --如果没有exit 则会死循环
     --while循环
     while condition loop
       statement1;
         statement2;
       end  loop;
     --for循环
     for counter in [reverse] lower_bounder ..upper_bounder   loop
         statement1;
         statement2;
       end loop;
       --counter循环控制变量
       --lower_bounder 起始值
       --upper_bounder停止值
       --reverse 自动减一  默认是加一
View Code

异常处理:

  异常是PL/SQL的表示符,默认情况下,发送异常就会终止PL/SQL块执行 ,通过引入异常就可以看情况而定

 oracle 提供了预定义异常,非预定义异常和自定义异常.

   结构:

 exception
        when exception1[or exception2]  then
          statement1; 
           statement1; 
           .......
           when exception3[or exception4]  then
          statement1; 
           statement1; 
           .......
           [when others then]
           statement1;
           .....
View Code

预定义异常:

  1,  case_not_found

在case语句中,如果在when中没有找到,又没有else语句,就会隐含触发

  2,  cursor_already_open

当打开已经打开的游标,会隐含触发

  3,invalid_number

 不能有效的把字符串转换为数字时,会隐含触发

  4,too_many_rows

 当执行select into时如果返回超过一行,会触发 

   5,zero_divide

除数为0,触发异常

   6,no_data_found

执行select into未返回行时,或者索引表未初始化元素时,会隐含触发该异常

自定义异常:

 declare 
             e_declare exception ;
            begin
                sql 语句;
                if sql%notfound then
                  raise  e_declare;
                  end if; 
                exception 
                  when  e_declare then
                     statement1; 
              end;
View Code

游标:

当在PL/SQL块中执行查询(select) 和数据操纵语句(DML) 时,oracle会为其分配上下文区,游标是指向上下文区的指针,执行查询(select) 和数据操纵语句(DML)  oracle会分配隐含游标

 显示游标:

 declare
              cursor emp_cursor[(id number )] --定义游标
              is
              begin
                open emp_cursor [(id number)];  --打开游标
                loop
                    fetch emp_cursor into variable1,variable1...;  --用于指定接收游标数据的变量
                   fetch emp_cursor into bulk collect into collect1 ,....;--用于指定接收游标结果集合的变量
                  end loop;
                  close emp_cursor;--关闭游标
                end;
View Code

 属性:

%isopen 判断游标是否打开 true:false

%found 检查是否从结果集中提取数据 true:false

%not found 与%found属性相反

rowcount 返回当前行数止已经提取到的实际函数

    

原文地址:https://www.cnblogs.com/shuaif/p/3496619.html