• PL/SQL笔记(1)-流程控制,循环,异常,块


    流程控制

    1.If,then,else,elsif(不是elseif)

    if a='1' then
      null;
    endif;

    image

    image

    image

    2.Case

    简单case表达式:

    image

    搜索型Case表达式:

    image

    3.goto语句

    begin
    if true then
      goto label2;
    end if;
    <<label1>>
      SYS.DBMS_OUTPUT.PUT_LINE('label1');
      <<label2>>
      SYS.DBMS_OUTPUT.PUT_LINE('label2');
    end;

    循环控制

    简单循环: Exit,Exit when condition退出循环

    image

    while循环:

    image

    for循环:两种,一个是面向数值的for,一种是面向游标的for

    面向数值:   (..)是范围操作符,1..5表示1到5

    image

    begin
    FOR j IN 1..5 LOOP
        dbms_output.Put_line(j);
      END LOOP;
    END;
    end;

    面向游标:

    image

    declare
    cursor myCursor is select * from ouser;
    begin
    FOR s IN myCursor LOOP
        dbms_output.Put_line(s.userid);
      END LOOP;
    END;

    continue,continue when语句

    结束本轮循环;

    --只输出偶数
    begin
    FOR j IN 1..100 LOOP
        Continue when Mod(j,2)=1;
        dbms_output.Put_line(j);
    END LOOP;
    end;

    异常处理

    1.命名异常和匿名异常

    命名异常有名字,匿名异常只有异常代码和消息

    SQLCODE函数可以获取最后一个异常的异常代码,SQLERRM:异常消息

    declare
    myexception exception; --声明一个命名异常
    v_row Sys_ACC_User%RowType;
    Pragma EXCEPTION_INIT (myexception, -20002);  --将一个命名异常和一个异常代码绑定
    begin
      select * into v_row from Sys_ACC_User where rownum=1;
      raise myexception;   --手动抛出异常
      RAISE_APPLICATION_ERROR(-20001,'这是一个匿名异常,我没有名字');  --手动抛出一个匿名异常
      Exception
        when no_data_Found then  --捕获名为no_data_found的异常
          dbms_output.Put_line('not data found'||'异常代码:'||SQLCODE||'  异常消息'||SQLERRM);
        when myexception then  --捕获名为 myexception的异常
          dbms_output.Put_line('myexception'||'异常代码:'||SQLCODE||'  异常消息'||SQLERRM);
        when others then --其他命名异常和匿名异常在这里捕获
          dbms_output.Put_line('异常代码:'||SQLCODE||'  异常消息'||SQLERRM);
    end;

     

    Oracle块:

    块组成:块头,声明单元,执行单元,异常处理单元

    函数,存储过程均为块结构,命名块

    image

    create or replace function WordCount(str in varchar2)return number --块头
    is
      numCount number default:=0;--声明单元
      begin --执行单元
      return Length(LTrim(str,'0'));
      Exception --异常处理单元
        when others then:
          SYS.DBMS_OUTPUT.PUT_LINE('error');
    end ;

    匿名块

    匿名块没有块头

    image

    declare  --声明单元
      v_n1 varchar2(100);
    begin
      --执行单元
      v_n1:='20';
      SYS.DBMS_OUTPUT.PUT_LINE(v_n1);
      exception --异常处理单元
        when others then
          SYS.DBMS_OUTPUT.PUT_LINE('error');
    end;

    其他:

    1.转义: q’<s’d>’,表示为: s’d  ,<和>必须成对出现,可用(),{},[]等代替

    2.Function必须返回值,不想返回值的用Procedure

    3.如果Procedure有参数(In/Out),调用方式: ProcedureName(param1,param2);如果procedure没有参数,则直接: ProcedureName或者ProcedureName(),Function类似…

    4.空字符 ’’is Null  =>true

    zhxjdwh:http://www.cnblogs.com/zhxj/

  • 相关阅读:
    javascript中的闭包、模块与模块加载
    jQuery源代码学习之九—jQuery事件模块
    javascript中的正则表达式学习
    javascript中的真假值、数据类型判断以及+的特殊用法
    jQuery源代码学习之八——jQuery属性操作模块
    使用RESTful风格,开发Web接口
    SpringBoot实战(1):搭建第一个SpringBoot项目
    Java面试考点解析(3)-- 数据库篇、Spring+MyBatis整合
    Java面试考点解析(2)-- Web开发、Spring、SprinMVC
    Java面试考点解析(1)-- 基础知识篇
  • 原文地址:https://www.cnblogs.com/zhxj/p/5130785.html
Copyright © 2020-2023  润新知