• Oracle的pl/sql编程语言


    学习笔记:

    ##pl/sql编程语言

        * 简介:
            * pl/sql编程语言是对sql语言的扩展,使得sql语言具有过程化编程的特性。
            * pl/sql编程语言比一般的过程化编程语言,更加灵活高效。
            * pl/sql编程语言主要用来编写存储过程和存储函数等。

        * 声明方法

    declare 
    begin
    end;


    --开启dbms_output输出
    set serveroutput on
    --声明方法
    declare 
      i number(2) := 10;
      s varchar2(10):= '小明';
      ena emp.ename%type; --引用型变量
    begin
      dbms_output.put_line(i);
      dbms_output.put_line(s);
      select ename into ena from emp where empno = 7788;
      dbms_output.put_line(ena);
    end;
    /

        * pl/sql中的if判断
            * 声明方法

    declare
    begin
      if then
      elsif then
      else
      end if
    end;

            * 案例
            * 输入小于18的数字,输出未成年;输入大于18小于40的数字,输出中年人;输入大于40的数字,输出老年人

    declare
      i number(3) := ⅈ
    begin
      if i<18 then
        dbms_output.put_line('Young people');
      elsif i<40 then
        dbms_output.put_line('Middle-aged person');
      else
        dbms_output.put_line('Aged');
      end if;
    end;

        * pl/sql中的循环
            * 1. while循环
                * 声明方法

    declare
    begin
      while loop
      end loop;
    end;

                * 案例:输入1到10

    declare
      i number(2) := 1;
    begin
      while i<= 10 loop
        dbms_output.put_line(i);
        i := i+1;
      end loop;
    end;

            * 2. exit循环(常用)
                * 声明方法

    declare
    begin
      loop
        exit when ;
      end loop;
    end;

                 * 案例:输入1到10

    declare
     i number(2) := 1;
    begin
      loop
        exit when i>10;
        dbms_output.put_line(i);
        i := i+1;
      end loop;
    end;


            * 3. loop循环
                * 声明方法

    declare
    begin
      for i in 1..10 loop
      end loop;
    end;

                * 案例:输入1到10

    declare
     i number(2) := 1;
    begin
      for i in 1..10 loop
        dbms_output.put_line(i);
        end loop;
    end;

            *注意:
                *三个循环结束皆为end loop;(end先行)
                * exit循环中exit when ; 后面有一个封号
                * 在循环loop开始时都不需要加封号
                * end结束一定有封号


        * pl/sql中的游标
            * 概念:游标可以存放多个对象,多个记录。
            * 案例1:输出emp表中所有员工的姓名

    declare
      cursor c1 is select * from emp;
      emprow emp%rowtype;
    begin
      open c1;
        loop
          fetch c1 into emprow;
          exit when c1%notfound;
          dbms_output.put_line(emprow.ename);
        end loop;
      close c1;
    end;

            * 案例2:给指定部门员工涨100块工资(带参数的游标)

    declare
      cursor c2(eno emp.deptno%type) 
      is select empno from emp where deptno = eno;
      en emp.empno%type;
    begin
      open c2(10);
        loop
          fetch c2 into en;
          exit when c2%notfound;
          update emp set sal=sal+100 where empno=en;
          commit;
        end loop;
      close c2;
    end;

    谢谢观看!

  • 相关阅读:
    ubuntu操作技巧
    电机
    哈佛结构与冯若依曼结构
    linux command ------ ls
    Frames of Reference参考框架
    What Are Tango Poses?Tango姿态是什么?
    Google Tango Java SDK开发:Motion Tracking 运动追踪
    Google Tango Java SDK开发:Configure and Connect 配置和连接
    gradle
    Google Tango service outdated谷歌Tango的服务过时了
  • 原文地址:https://www.cnblogs.com/edcoder/p/11961782.html
Copyright © 2020-2023  润新知