zoukankan      html  css  js  c++  java
  • pl/sql 创建存储过程

    1、存储过程创建格式:

      create [or replace] procedure procedure_name(参数)
      as/is
        声明部分
      begin
          执行部分
      [exception
          异常处理部分]
      end;

      注:(1).中括号为可选部分,即在一个存储过程中可以有也可以没有。

        (2).如果存储过程没有参数不要用括号。

    2.调用存储过程:

      格式:execute/exec procedure_name

      举例调用:

      begin

        procedure_name(参数);

      end;

      

    3.学习创建存储过程:

      (1).创建一个无参存储过程
        create procedure proc_test
        is
        begin
          dbms_output.put_line('我的第一个存储过程');
        end;

        调用:

        begin
          proc_test;
        end;

      (2).创建一个存储过程用于计算一个数的平方。

        create or replace procedure proc_test01(a in number,b out number)
        is
        begin
          b:=a*a;
        end;

        执行:

        declare
        b number;
        begin
          proc_test01(5,b);
            dbms_output.put_line('5的平方是:'||b);
        end;

      (3).创建一个存储过程,要求输入指定员工编号,使该员工的工资增加20%,要有异常处理;

      create or replace procedure proc_add_sal(eno employee.empno%type)
      as
      v_sal employee.sal%type;
      sal_is_null exception;
      begin
        select sal into v_sal from employee where empno=eno;
        if v_sal is null
          then
            raise sal_is_null;
        else
          update employee set sal=sal*1.2 where empno=eno;
        end if;
        exception
          when sal_is_null
           then
              dbms_output.put_line('雇员工资不存在');
            when no_data_found

       then
              dbms_output.put_line('员工编号不存在');
           when others

        then
                dbms_output.put_line('出现其它异常');
    end;
      
      begin
        proc_add_sal(eno=>9999);
      end;

    4.指定执行存储过程的权限给其它用户:

      grant execute on procedure_name to user/public;

    5.使用存储过程注意事项:

      (1).必须包含声明和可执行部分;

      (2).调用存储过程有三中参数指定方式:按位置指定,按名称指定,混合指定;

      (3).参数类型有三种:in,out,in out;

      (4).不可以使用DDL;

      (5).变量类型用引用类型;

      (6).存储过程最好声明异常处理。

    如果不学习,活着干什么
  • 相关阅读:
    【NOIP2007】守望者的逃离
    20200321(ABC)题解 by 马鸿儒 孙晨曦
    20200320(ABC)题解 by 王一帆
    20200319(ABC)题解 by 王一帆 梁延杰 丁智辰
    20200314(ABC)题解 by 董国梁 蒋丽君 章思航
    20200309(ABC)题解 by 梁延杰
    20200307(DEF)题解 by 孙晨曦
    20200306(ABC)题解 by 孙晨曦
    20200305(DEF)题解 by 孙晨曦
    20200303(ABC)题解 by 王锐,董国梁
  • 原文地址:https://www.cnblogs.com/wdlove/p/3924352.html
Copyright © 2011-2022 走看看