• 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).存储过程最好声明异常处理。

    如果不学习,活着干什么
  • 相关阅读:
    What's the difference between tilde(~) and caret(^) in package.json?
    腾迅股票数据接口
    dddd
    https://wrapbootstrap.com
    http://tpl.amazeui.org/
    http://www.ajaxload.info/
    基于Sublime Text搭建Python IDE
    IDAPython: importing “site” failed
    IDA_Python命令行使用
    “error LNK2019: 无法解析的外部符号”之分析
  • 原文地址:https://www.cnblogs.com/wdlove/p/3924352.html
Copyright © 2020-2023  润新知