Oracle-流程控制语句

------------------  条件 分支  
---- if   boolean  then  代码块   end if;
---- if   boolean  then  代码块   else  代码块   end if;
---- if   boolean  then  代码块   elsif  boolean 代码块   elsif  boolean 代码块...  else 代码块  end if;
begin
  if  180>150 then
    dbms_output.put_line('if 执行   180 >150') ;
  end if;
  if  180<150  then
     dbms_output.put_line('else 执行   180 >150 错啦') ;
  end if;
end;


begin
  if  180>150 then
    dbms_output.put_line('if 执行   180 >150') ;
  else 
     dbms_output.put_line('else 执行   180 >150 错啦') ;
  end if;
end;

----------------- 键盘输入   3个值  求最大值  
declare 

  a  number(4):=&a;
  
begin
  dbms_output.put_line('键盘输入 :'||a) ;

end;

---3个值  求最大值   
---- 方式一
declare  
  a  number(4):=&x;
  b  number(4):=&y;
  c  number(4):=&z; 
begin
  if a>b then 
     if a>c then
        dbms_output.put_line(a);
     else 
        dbms_output.put_line(c);
     end if;
  else  ---  b>=a 
     if b>c then
        dbms_output.put_line(b);
     else 
        dbms_output.put_line(c);
     end if;
  end if;


end;

---- 方式二
declare  
  a  number(4):=&x;
  b  number(4):=&y;
  c  number(4):=&z; 
begin
  if a>=b and  a>=c   then 
      dbms_output.put_line(a);
  elsif  b>=a  and b>=c then 
      dbms_output.put_line(b);
  else 
      dbms_output.put_line(c);
  end if;


end;
-------------- 推荐思路  
declare  
  a  number(4):=&x;
  b  number(4):=&y;
  c  number(4):=&z; 
  m  number(4);---最大值
begin
  m:=a;--- 假设 a 是最大值 
  if m<b then 
     m:=b;
  end if;

  if m<c then 
     m:=c;
  end if;
  dbms_output.put_line(m);
end;
-------------case   --------------------
-- 等值判定 
--case  表达式  when  值1  then  执行体  when 值2  then  执行体 。。。 else  执行体  end case
-- 不等值  
--case     when  表达式1  then  执行体  when 表达式2  then  执行体 。。。 else  执行体  end case---

declare
   v_day  number(1) :=&n;
begin
  case   v_day
       when 1 then dbms_output.put_line('周一');--  
       when 2 then dbms_output.put_line('周二');--
       when 3 then dbms_output.put_line('周三');--
       when 4 then dbms_output.put_line('周四');--
       when 5 then dbms_output.put_line('周五');--
       when 6 then dbms_output.put_line('周六');--
       when 7 then dbms_output.put_line('周天');--
       else   dbms_output.put_line('超神');
      end  case;
end;

-- 1 接受键盘输入   年龄   根据年龄  输出对应的状态
declare
   v_age  number :=&n;
begin
  
  case 
      when v_age<=10 then dbms_output.put_line('碎娃');--  
      when v_age<=20  then dbms_output.put_line('小伙子'); -- 弱冠
      when v_age<=30  then dbms_output.put_line('愤青');-- 而立   不惑    知天命  花甲  古稀  
      else                 dbms_output.put_line('这辈子就这样啦'); --
   end  case;
  
end;
---------------
/*
输入员工编号 根据员工的工资 划分等级
0-1000     村民    
1000-2000  还是村民
2000-3000  守卫 
3000-4000  神
4000-5000  狼人
           法官
*/
--- loop     

----  loop   。。。。循环体   exit when boolean(退出条件)  end  loop;   

----输出1-10
declare  
 v_index number(2) :=1;
begin
  loop        ----------类比   java  do while  思想一致
    dbms_output.put_line(v_index);
    v_index:=v_index+1;
  
    exit when v_index=11;
  end loop;
end;
  

declare  
 v_index number(2) :=1;
begin
  loop        ----------类比   java  do while  思想一致
    exit when true;
    dbms_output.put_line(v_index);
    v_index:=v_index+1;
  end loop;
end;

---------- 1....10 之和
declare  
 v_index number(2) :=1;
 v_sum number(2):=0;
begin
  loop        ----------类比   java  do while  思想一致
    v_sum:=v_sum+v_index;
    
    v_index:=v_index+1;
    
   exit when   v_index =11;
  end loop;
  
   dbms_output.put_line(v_sum);
end;

------ 将工资 大于 3000  或者  员工的 job 是  CLERK  的员工信息输出
declare 
   type emp_table_type is table of emp%rowtype index by binary_integer;
   
   v_emp_table emp_table_type;
   
   v_index number(2):=1;--- 循环变量 
begin
   --- 全部查询
   select *  bulk collect into   v_emp_table  from emp   ;
   
   loop
     
     if v_emp_table(v_index).sal>3000 or v_emp_table(v_index).job='CLERK' then
        dbms_output.put_line(v_emp_table(v_index).ename||v_emp_table(v_index).sal||v_emp_table(v_index).job);
     end if;
     exit when  v_index=v_emp_table.count; -- 利用 数量 最大数
     v_index:=v_index+1;
   end loop;
end;  
 select *    from emp  where sal>3000 or job='CLERK';

---- 思路二   exists-- 将工资 大于 3000  或者  员工的 job 是  CLERK  的员工信息输出
declare 
   type emp_table_type is table of emp%rowtype index by binary_integer;
   
   v_emp_table emp_table_type;
   
   v_index number(2):=1;--- 循环变量 
begin
   --- 全部查询
   select *  bulk collect into   v_emp_table  from emp   ;
   
   loop
     exit  when  not  v_emp_table.exists(v_index);
     if v_emp_table(v_index).sal>3000 or v_emp_table(v_index).job='CLERK' then
        dbms_output.put_line(v_emp_table(v_index).ename||v_emp_table(v_index).sal||v_emp_table(v_index).job);
     end if;
     v_index:=v_index+1;
   end loop;
end;  
----- loop      9*9 乘法表  
begin
  dbms_output.put('123');
  dbms_output.put_line('123');
  dbms_output.put('123');
  dbms_output.put_line('');
end;  

declare 
   rowinfo  number(2) :=1;
   colinfo number(2);
begin
  loop
    colinfo:=1;
    loop
       dbms_output.put(rowinfo||'*'||colinfo||'='||colinfo*rowinfo||'     ');
       colinfo:=colinfo+1;
       exit when colinfo>rowinfo;
    end loop;
    dbms_output.put_line('');
    rowinfo:=rowinfo+1;
      exit when rowinfo>9;
    
  end loop;

end;

---while
  --- while  boolean  loop  循环体   [exit  when ] end loop
---1 ..10
declare 
 v_index number :=1;
begin
  while  v_index<=10  loop
     dbms_output.put_line(v_index);
     v_index:=v_index+1;
  
  end loop;
  v_index:=1;
    while  true  loop
     dbms_output.put_line(v_index);
     v_index:=v_index+1;
      --退出条件
     exit when v_index>10;  
      
  end loop;
end;
  
--  for  
---  for 循环变量  in  beginValue.. endValue loop  循环体  end loop;

begin
  for  i  in  1..10 loop   --- i  循环变量  从1 开始   到10结束   每次自动加一
         dbms_output.put_line(i);
  end loop;
end;

---  for 循环变量  in  reverse   beginValue.. endValue loop  循环体  end loop; -- 逆向输出
begin
  for  i  in  reverse  1..10 loop   --- i  循环变量  从1 开始   到10结束   每次自动加一
         dbms_output.put_line(i);
  end loop;
end;

----- for  9*9
***   1  3
**    2  2
*     3  1
declare  
   v_row number(2) :=&n;
begin
  for   i in 1..v_row loop
     for  j  in  1..(v_row+1-i) loop
        dbms_output.put('*');
     end loop;
  dbms_output.put_line('');
  end loop;
end;
----  
  *     1   1    2 
 * *    2   2    1
* * *   3   3    0
------
declare  
   v_row number(2) :=&n;
begin
  for   i in 1..v_row loop
     for  j  in reverse  1..(v_row-i) loop
        dbms_output.put(' ');
     end loop;
     for  j  in reverse  1..i loop
        dbms_output.put('* ');
     end loop; 
  dbms_output.put_line('');
  end loop;
end;
-----------for  
----- 输出员工信息    使用记录类型  (ename ,job  ,deptno) 表类型 结合   
---输出   员工姓名  工作  部门编号  (10  技术部  20  财务部  30 销售部 40 伐木部 )
declare
   type emp_record is record( 
   ename emp.ename%type,
   job   varchar2(10),
   deptno number(2)
   );
   type emp_table_type is table of emp_record index by binary_integer;
   
   v_empinfos emp_table_type;
   
   v_dname varchar2(10);
begin
   select ename,job ,deptno bulk collect into v_empinfos  from emp;
   
   for i in 1..v_empinfos.count loop
       case v_empinfos(i).deptno
         when 10 then 
           v_dname:='技术部';
         when 20 then 
           v_dname:='财务部';  
         when 30 then 
           v_dname:='销售部';
         else 
            null;
           
        end  case ;
       dbms_output.put_line(v_empinfos(i).ename||v_empinfos(i).job||v_empinfos(i).deptno||v_dname);
   end loop;  

end;

-----------goto 标记   << 标记 >>
declare
 v_num  number(1):=&n;

begin
    for i in 1..10  loop
       dbms_output.put_line(i);
       if  v_num=i then 
        dbms_output.put_line('进入if语句  ||i');   
        goto  info;----  ---- 去一个指定 标记位 
       end if;
       end loop;
     dbms_output.put_line('for 正常结束 '); 
     goto endinfo;

     <<info>>
      dbms_output.put_line('进入info  标记位置 '); 

      <<endinfo>>
      dbms_output.put_line('这是结尾 '); 
end;

---- 批量添加

declare  
  type  id_type is table of  number(4) index by binary_integer;
  type  name_type is table of  varchar2(20) index by binary_integer;
  v_ids id_type;
  v_names name_type;
begin
  select  empno,ename  bulk collect  into v_ids,v_names  from emp;
   
  --- 批量添加
   forall i  in  1..v_ids.count  
          insert into employee (empno,ename)  values(v_ids(i),v_names(i));
  
end;

---truncate  table employee

select *from employee

------------------  练习题
---1. 从键盘输入一个数字,利用for in 循环,输出一个该数字的所有约数。
---比如 12的约数是: 1 2 3 4 6 12
---比如 20的约数是: 1 2 4 5 10 20
declare
   a number(2):=&a;
   begin 
     for i in 1..a loop
       if mod(a,i)=0 then
         dbms_output.put_line('约数为'||i);
         end if;
         end loop;
       end;


---2.从键盘输入一个数字,统计该数字一共有多少个约数!
declare
   a number(2):=&a;
   b number(2):=0;
   begin
     for i in 1..a loop
     if mod(a,i)=0 then
         b:=b+1;
         end if;
         end loop;
           dbms_output.put_line('个数为'||b);
    end;     


---3.从键盘输入一个数字,判断该数字是不是质数。
declare
   a number(2):=&a;
   b number(2):=0;
   begin
     for i in 1..a loop
     if mod(a,i)=0 then
         b:=b+1; 
         end if;
         end loop;
     if  b=2 then 
      dbms_output.put_line('质数'||a);
     end if;
    end; 

declare
   a number(2):=&a;
   b number(2):=0;
   begin
     for i in 2..a-1 loop
     if mod(a,i)=0 then
       b:=1;
         exit;    
         end if;
         end loop;
     if  b=0 then 
      dbms_output.put_line('质数---'||a);
     end if;
    end; 
-------   1-100 之内的所有质数

declare
   b number(2);
   begin
     for a  in 2..100 loop
       b:=0;
       for i in 2..a-1 loop
       if mod(a,i)=0 then
         b:=1;
           exit;    
           end if;
           end loop;
       if  b=0 then 
        dbms_output.put_line('质数---'||a);
       end if;
     end loop;
end; 


---4. 从键盘输入一个数字,
    ---如果输入了1,则计算2的结果      
    ---如果输入了2,则计算2+22的结果     
    ---如果输入了3,则计算2+22+222的结果
    ---如果输入了4,则计算2+22+222+2222的结果
    ---如果输入了5,则计算2+22+222+2222+22222的结果
    ---.....
declare  
   t number :=&n;
   v_sum number :=0;
   v_n   number:=0;---当前加的数字
begin
    for  i  in 1..t  loop
      v_n:= v_n*10+2;---要前加的当前数字
      v_sum:=v_sum+v_n;
    end loop;
     dbms_output.put_line('和---'||v_sum);
end;
  
declare  
   t number :=&n;
   v_sum number :=0;
   v_n   number:=0;---当前加的数字
begin
    for  i  in 1..t  loop
      v_n:=  2*power(10,i-1)+v_n;--    v_n*10+2    ;---要前加的当前数字
      v_sum:=v_sum+v_n;
    end loop;
     dbms_output.put_line('和---'||v_sum);
end;
  
declare
  input number:=&a;
  ssum number:=0;
  s varchar2(100):='';
begin
    --- 时间复杂度 
    
    ---空间  
   
  
    for j in 1..input loop  
      s:=concat(s,'2');
      ssum:=ssum+s;
    end loop;  
    
  dbms_output.put_line(ssum);
end; 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值