zoukankan      html  css  js  c++  java
  • pl/sql

    一、pl/sql编程语言

          --复合索引触发规则,复合索引第一列位有限检索列

          --必须包含优先检索列中的值,才会触发


         1.声明方法

    declare 
       i number(10) :=10;
       s varchar2(10) :='小红';
       ena emp.ename%type; --引用型的变量
       emprow emp%rowtype;--记录型变量
    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);
      select * into emprow from emp where empno=7788;
       dbms_output.put_line(emprow.ename || '工作为' || emprow.job);
    end;

       2.pl/sql中的if判断   

    declare
        i number(3) :=&i;  -- &i输入一个数
    begin
      if i<18 then
       dbms_output.put_line('未成年');
      elsif i<40 then
       dbms_output.put_line('成年人');
      else
        dbms_output.put_line('老年人');
      end if;
    end;

         3.pl/sql中的循环

    --while
    declare
       i number(2) :=1;
    begin
      while i<11 loop
        dbms_output.put_line(i);
        i :=i+1;
      end loop;
    end;
    
    --exit循环
    declare
       i number(2) :=1;
    begin
       loop
         exit when i>10;
          dbms_output.put_line(i);
         i := i+1;
       end loop;
    end;
    
    --for循环
    declare
    begin
      for i in 1..10 loop
         dbms_output.put_line(i);
      end loop;
    end;

          4.游标

              - -游标:可放多个记录和对象,即多行记录。

    --输出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;
    --给指定部门员工涨工资
    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;
    select * from emp where deptno =10

          5.存储过程

           - - 存储过程是提前已经编译好的一段pl/sql语言,放置在数据库端可供人直接调用,是固定步骤的业务。

             

    --in类型的参数
    --
    给指定员工涨钱 create or replace procedure p1(eno emp.empno%type) is begin update emp set sal=sal+100 where empno=eno; commit; end; --测试p1 declare begin p1(7788); end;
    ---out类型的参数的使用
    --通过存储过程计算指定员工的年薪
    create or replace procedure p_yearsal(eno emp.empno%type,yearsal out number)
    is
      s number(10);
      c emp.comm%type;
    begin
      select sal*12,nvl(comm,0) into s,c from emp where empno=eno;
      yearsal :=s+c;
    end; 
    
    --测试
    declare 
       yearsal number(10);
    begin
      p_yearsal(7788,yearsal);
      dbms_output.put_line(yearsal);
    end;

       

          注:in和out的区别:

              凡是涉及到into查询语句赋值或者:=赋值操作参数,都必须使用out来修饰。

        6.存储函数

               语法:

              

    --通过存储函数计算指定员工的年薪
    create or replace function f_yearsal(eno emp.empno%type) return number
    is
      s number(10);
    begin
      select sal*12+nvl(comm,0) into s from emp where empno=eno;
      return s;
    end;
    
    
    --测试f_yearsal
    
    declare
      s number(10);
    begin
      s :=f_yearsal(7788);
      dbms_output.put_line(s);
    end;

    7.存储过程和存储函数的区别

         语法区别:关键字不一样。

          本质区别:存储函数右返回值,存储过程没有返回值。

                            如果存储过程想实现右返回值的业务,我们就必须使用out类型的参数。

                            我们可以使用存储函数返回值的特性,来自定义函数,而存储过程不能自定义函数。

    --使用存储函数实现提供部门编号,输入一个部门名称
    create or replace function fdna(dno dept.deptno%type) return dept.dname%type
    is
      dna dept.dname%type; 
    begin
      select dname into dna from dept where deptno=dno;
      return dna;
    end;
    --使用fdna存储函数实现要求:查询出员工姓名,员工所在部门
    select e.ename,fdna(e.deptno) from emp e;    

    8.触发器

         1)触发器就是 指定一个规则,在做增删改操作时,只要满足规则,自动触发,无需调用。

         2)分类:

                *  语句级触发器:不包含for、each、 row的触发器。

                *  行级触发器:包含for、each、 row的触发器。

                     -  - 加上 for each new是为了使用 :old或者 :new 对象或者一行记录

                

          3)语句级触发器:          

    --插入一条记录,输出一个员工入职
    create or replace trigger t1
    after        --之后执行
    insert        --插入操作
    on person   --作用于pereson表
    declare
    begin
      dbms_output.put_line('员工入职');
    end;
    
    --触发t1
    insert into person values(1,'小明');
    commit;

          4) 行级触发器

    --不能给员工降薪
    --抛出异常 raise_application_error(-20001~20009之间,'错误提示信息');
    create or replace trigger t2
    before
    update
    on emp
    for each row
    declare
    
    begin
      if :old.sal>:new.sal then
        raise_application_error(-20001,'不能降薪');
      end if;
    end;
    
    
    --触发t2
    update emp set sal=sal-1000 where empno=7788;
    commit;

       

       案例:触发器实现主键自增      

    --触发器实现主键自增【行级触发器】
    create or replace trigger auid
    before
    insert
    on person
    for each row
    declare
    
    begin
      select s_person.nextval into :new.pid from dual;
    end;
    
    --使用audi实现主键自增
    insert into person (pname) values('小李');
    commit; 

              

  • 相关阅读:
    js 删除数组的某一项或者几项的方法
    JavaScript 面向对象的程序设计(一)之理解对象属性
    iview 之 穿梭框 transfer
    iview2.0 bug之+8 区的 DatePicker
    js Object.keys 用法
    js map 、filter 、forEach 、every、some 的用法
    iview tree 之如何获取已勾选的节点
    js 中常见的深拷贝的方法
    ES6新特性
    Amazon2014在线笔试 第三题
  • 原文地址:https://www.cnblogs.com/cqyp/p/12640733.html
Copyright © 2011-2022 走看看