• oracle存储过程创建-调试-调用-删除(二)


    上一篇文章已经介绍了存储过程简单的创建,调用,调试和删除。这篇文章将会主要讨论一下存储过程中选择循环判断等的用法。

    存储过程中变量的定义和赋值

    在存储过程里,我们除了可以使用参数,还可以定义一些变量来进行操作。

    第一种赋值方式 ::=

    1. 定义的变量要写在as或is关键字后面,begin前面。

    2. 定义的变量区别于参数的是,需要指定长度。

    3. 定义完的参数可以在begin中对其进行赋值,oracle中赋值的动作不是 = 而是 := 。

    4. 参数为in类型的不允许再次赋值了。

    5. 下面的参数num虽然存在,但是下面的代码中没有地方引用,所以编译的时候会提醒  ‘已编译但有警告’。

    6. commit提交一定要写,否则无法成功完成insert操作。

    7. 为字符类型的变量赋值的时候两边要用单引号,数值型则不用。

     1 create or replace procedure test3(num number) as
     2 age integer;--integer不需要指定长度
     3 names varchar2(20);--varchar2 需要指定长度 
     4 sex integer;
     5 begin
     6   age:=0;--为上面定义的变量赋值不是用 = 而是用 :=
     7   names:='张三';-- 为字符型数据赋值两边要加上单引号
     8   sex:=1;
     9   --num:=1;   这里要注意 参数类型为in的不允许再次赋值了
    10   insert into t_user values (sys_guid(),names,age,sex,'12541554040',sysdate);
    11   commit;
    12   dbms_output.put_line('ok');
    13 end;

    第二种赋值方式 :select into

    1. select into可以为一个或者多个变量赋值。

    2. select into为多个变量赋值的时候需要赋值的变量和查出的变量,顺序要相同。

    3. dbms_output.put_line类似于java中的System.out.println();

     1 create or replace procedure test4 as
     2 age integer;
     3 sex integer;
     4 begin
     5   select age into age from t_user where name = '张三';--单个变量赋值
     6   select age,sex into age,sex from t_user where name = '张三';--多个变量赋值
     7   commit;
     8   dbms_output.put_line('ok');
     9   dbms_output.put_line('年龄:'||age||',性别:'||sex);
    10 end;

    存储过程中的选择语句

    if选择:

    (关键字:if,elsif,else,then,end if ) 注意:

    1. oracle中的 '否则如果' 写法是elsif 不同于java中的else if

    2. if或者是elsif后面都要跟then,不同于java

    3. oracle中的这种关键字一般都需要成对出现,所以结尾一定要有end if

     1 create or replace procedure test5(num in number) as
     2 begin
     3   if num>0 then
     4     dbms_output.put_line('num>0');
     5   elsif num <0 then 
     6     dbms_output.put_line('num<0');
     7   else
     8     dbms_output.put_line('num=0');
     9   end if;
    10 end;

    case when选择(类似于if)

    注意:

    1. case when 最后要以end case结尾

    2. 每个when后面都要接then

     1 create or replace procedure test6(num in number) as
     2 begin
     3   case num
     4     when 1 then
     5       dbms_output.put_line('num=1');
     6     when 2 then
     7       dbms_output.put_line('num=2');
     8     else
     9       dbms_output.put_line('num=3');
    10   end case;
    11 end;

    存储过程中的循环语句

    存储过程中的基本循环loop循环

    关键字: loop,exit when,end loop 注意:

    1. loop循环以loop开始 end loop结束。

    2. 此段代码的意思是从a等于0开始一直到a>10结束 循环输出。

    3. exit when的意思是当什么情况下退出。

     1 create or replace procedure test7 as
     2 a integer;
     3 begin
     4   a:=0;
     5   loop
     6      dbms_output.put_line('num='||a);
     7      a:=a+1;
     8   exit when
     9      a>10;
    10   end loop;
    11 end;

    存储过程中的while循环

    关键字: while,loop,end loop

    1. 此段代码的意思是如果a<10就循环输出一次a的值。

    2. while后面要接条件和loop关键字。

    3. 最后要以end loop结尾。

    1 create or replace procedure test8 as
    2 a integer;
    3 begin
    4   a:=0;
    5   while a<10 loop
    6      dbms_output.put_line('num='||a);
    7      a:=a+1;
    8   end loop;
    9 end;

    存储过程中的for循环

    关键字: for,in,loop,end loop

    1. 此段代码的意思是将t_user表中查出的name列数据放到tname中并循环输出。

    2. 此段代码用到了游标cursor,不理解的可以暂时不考虑。

    3. aname类似于java中for循环的int i中的i。

    4. 如果想要从tname中获取name的值,需要用aname.name的方式来获取。

    create or replace procedure test9 as
    Cursor tname is select name from t_user; 
    begin
      for aname in tname LOOP
        dbms_output.put_line(aname.name); 
      end LOOP;
    end;

    持续更新!!

  • 相关阅读:
    计算机网络-TCP的三次握手与四次挥手
    计算机网络-XSS及CSRF攻击防御
    计算机网络-HTTP与HTTPS的区别
    装饰器模式和代理模式的区别
    23种设计模式总结
    单例模式详解
    常用设计模式总结
    PG-用户|角色管理
    PG-表空间管理
    TiDB-性能测试
  • 原文地址:https://www.cnblogs.com/flyinghome/p/12154707.html
Copyright © 2020-2023  润新知