zoukankan      html  css  js  c++  java
  • PL/SQL 关于循环的练习

    输出99乘法表
    declare
    begin
      for i in 1..9 loop
        for j in 1..i loop
        --不换行输出数据
          dbms_output.put(j||'*'||i||'='||(j*i));
        end loop;
        --换行输出
        dbms_output.put_line(null);
      end loop;
    end;
    效果:
    1*1=1
    1*2=22*2=4
    1*3=32*3=63*3=9
    1*4=42*4=83*4=124*4=16
    1*5=52*5=103*5=154*5=205*5=25
    1*6=62*6=123*6=184*6=245*6=306*6=36
    1*7=72*7=143*7=214*7=285*7=356*7=427*7=49
    1*8=82*8=163*8=244*8=325*8=406*8=487*8=568*8=64
    1*9=92*9=183*9=274*9=365*9=456*9=547*9=638*9=729*9=81
     
    输出等腰三角形
    declare
    begin
      for i in 1..10 loop
        --循环输出总数-行数个空格
        for j in reverse i..10 loop
          dbms_output.put(' ');
        end loop;
        --输出行数个*号和空格
        for x in 1..i loop
          dbms_output.put('* ');
        end loop;
        --换行
      dbms_output.put_line(null);
      end loop;
    end;
     
    效果:
              *
             * *
            * * *
           * * * *
          * * * * *
         * * * * * *
        * * * * * * *
       * * * * * * * *
      * * * * * * * * *
     * * * * * * * * * *
    四种循环方法遍历一个表
     
    --遍历college表
    --第一种方法,游标+for循环
     1  
     2 declare
     3 cursor coll_cursor is select * from college;
     4 begin
     5   for coll_info in coll_cursor loop
     6     dbms_output.put(coll_info.coll_id||'  ');
     7     dbms_output.put(coll_info.coll_name||'  ');
     8     dbms_output.put(coll_info.coll_score);
     9     dbms_output.put_line(null);
    10   end loop;
    11 end;
     
    --第二种方法 游标+while循环
     
     1 declare
     2   cursor coll_cursor is select coll_id,coll_name from college;
     3 id college.coll_id%type;
     4 name college.coll_name%type;
     5 begin
     6   open coll_cursor;
     7   fetch coll_cursor into id,name;
     8   while coll_cursor%found loop
     9     dbms_output.put(id||' ');
    10     dbms_output.put(name);
    11     dbms_output.put_line(null);
    12     fetch coll_cursor into id,name;
    13   end loop;
    14   close coll_cursor;
    15 end;
     
    --第三种方法 不定义游标,直接获取结果集里面的数据
     
    begin
      for coll_info in (select * from college) loop
        dbms_output.put(coll_info.coll_id||'  ');
        dbms_output.put(coll_info.coll_name||'  ');
        dbms_output.put(coll_info.coll_score);
        dbms_output.put_line(null);
      end loop;
    end;
     
    --第四种方法
     1 declare
     2  cursor coll_cursor is select * from college;
     3  coll_info college%rowtype;
     4 begin
     5   open coll_cursor;
     6   loop
     7     fetch coll_cursor into coll_info;
     8     exit when coll_cursor%notfound;
     9     dbms_output.put(coll_info.coll_id||'  ');
    10     dbms_output.put(coll_info.coll_name||'  ');
    11     dbms_output.put(coll_info.coll_score);
    12     dbms_output.put_line(null);
    13   end loop;
    14   close coll_cursor;
    15 end;
     
    结果:
    1013  燕山大学  575
    1001  清华大学  620
    1002  北京大学  600
    1003  武汉大学  550
    1004  华南科技大学  530
    1005  复旦大学  580
    1006  中山大学  620
    1007  华北理工大学  500
    1008  暨南大学  510
    1009  山东科技大学  620
    1014  哈尔滨工业大学  575
    1010  唐山学院  480
    1011  吉林大学  580
    1015  中国石油大学  565
     
     
  • 相关阅读:
    数“1”游戏
    第二次冲刺期_每日站立会议_个人记录_文档
    第一次冲刺期——每日站立会议——个人记录——文档
    团队绩效
    其他小组对我们的评价汇总
    Sprint评分表
    大作业项目冲刺阶段(一)
    丹佛机场行李系统Postmortem
    团队项目测试计划
    SCRUM报告(1)
  • 原文地址:https://www.cnblogs.com/anzhi/p/7568420.html
Copyright © 2011-2022 走看看