zoukankan      html  css  js  c++  java
  • PL/SQL的结构

      PL/SQL Developer是一个集成开发环境,专门开发面向Oracle数据库的应用。PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Language/SQL)。PL/SQL是Oracle数据库对SQL语句的扩展。在普通SQL语句的使用上增加了编程语言的特点,所以PL/SQL把数据操作和查询语句组织在PL/SQL代码的过程性单元中,通过逻辑判断、循环等操作实现复杂的功能或者计算。MySQL 也支持 PL/SQL 的,但支持Navicat Premium。

    一、组成:

    1、定义部分:

      定义一些常量、变量等;

    2、执行部分:(必须存在)

      包含要执行的sql语句;

    3、异常处理部分:

      捕获异常(可能出现的运行错误,并编写出错后的代码)。

    declare
     --变量声明部分
    begin
     --执行部分
    exception
     --异常处理部分
    End

    二、调试:

    使用set serveroutput on 命令设置环境变量;
    serveroutput为打开状态,从而使得pl/sql程序能够在SQL*plus中输出结果。
    在编写存储过程时,有时会用
    dbms_output.put_line将必要的信息输出,以便对存储过程进行调试,只有将serveroutput变量设为on后,信息才能显示在屏幕上。

    三、变量的定义:(类似java里面的匿名方法)

    1、格式:

    变量名 变量类型(oracle里面数据类型) [约束] default 默认值

    变量名 变量类型 [约束] [:=初始值]

    2、

    SQL> declare
      2    i number default 10;
      3  begin
      4    i:=i+10;
      5    dbms_output.put_line(i);
      6  end;
      7  /
    20
    PL/SQL procedure successfully completed

    四、流程控制语句:

    1、if语句:

    if 条件 then
    ...
    end if;
    
    
    
    if 条件 then
    ...
    else
    ...
    end if;
    
    
    if 条件 then
    ...
    elsif 条件 then
    ...
    end if;
    SQL> declare
      2      age number default 90;
      3      height number := 175;
      4      gender char(4) := '';
      5  begin
      6      if gender='' then
      7          dbms_output.put_line('你可以和女性结婚');
      8      end if;
      9  
     10      if height>170 then
     11          dbms_output.put_line('可以打篮球');
     12      else
     13          dbms_output.put_line('可以踢足球');
     14      end if;
     15  
     16      if age<20 then
     17          dbms_output.put_line('年轻小伙');
     18      elsif age <= 50 then
     19          dbms_output.put_line('年轻有为');
     20      elsif age <=70 then
     21          dbms_output.put_line('安享天伦');
     22      else
     23          dbms_output.put_line('佩服佩服');
     24      end if;
     25  
     26  end;
     27  /
    你可以和女性结婚
    可以打篮球
    佩服佩服
    PL/SQL procedure successfully completed

     2、while循环:

    while 条件 loop
    循环体
    end loop

    3、for循环:

    for 循环变量 in [reverse] 起始值..终止值 loop
        循环体
    end loop

    reverse 是倒序;注意起始值与终止值中间有两点

    4、loop循环语句:

    loop
    --循环体
    exit when 条件;
    end loop;
    SQL> declare
      2      i number :=0;
      3      total number :=0;
      4  begin
      5      loop
      6          i := i+1;
      7          total := total + i;
      8  
      9          exit when i>=100;
     10      end loop;
     11  
     12      dbms_output.put_line('总和'||total);
     13  
     14  end;
     15  /
    总和5050
    PL/SQL procedure successfully completed

    四、存储过程创建语句:

    存储过程就是有名字的plsql块!

    procedure就是存储过程!

    create or replace procedure 名称[(参数)]--此名称类似java里的方法名
    authid current_user|definer --以定义者还是调用者的身份运行
    is[不要加declare]
    --变量声明部分
    begin
    --主体部分,封装起来,然后调用的时候才开始执行,类似于调用方法!!!
    exception
    --异常部分
    end;
    SQL> create procedure a2(width int,height int)
      2      is
      3      area int:=0;
      4      begin
      5      area:=width*height;
      6      dbms_output.put_line('area is'||area);
      7      end;
      8  /
    Procedure created
    --调用存储过程--有参的!
    call a2(1,2);
  • 相关阅读:
    【iOS基础控件 6 】 汽车品牌展示 Model嵌套/KVC/TableView索引 <UITableView>
    RAC
    Magical Data Modelling Framework for JSON
    Xcode插件管理工具Alcatraz
    iOS8 Size Classes的理解与使用
    Storyboard、xib中的UIScrollView使用autolayout,使其能够滚动
    iOS事件响应链
    git一些常用的操作(转载)
    iOS本地数据存储(转载)
    使用react-native做一个简单的应用-04界面主框架
  • 原文地址:https://www.cnblogs.com/21-forever/p/11273838.html
Copyright © 2011-2022 走看看