【Oracle】Oracle PL/SQL编程简介2

来源:互联网 发布:自拍的淘宝图片 编辑:程序博客网 时间:2024/06/08 11:15

 PL/SQL编程:






如声明一个记录v_record,它和stu表具有相同的名称和数据类型:


如声明了游标提取sno,sname,sage,则可以使用%rowtype声明一个记录存储相同的信息;

[sql] view plaincopy在CODE上查看代码片派生到我的代码片
  1. declare  
  2. cursor cur is select sno,sname,sage from stu;  
  3. cursor_record cur%rowtype;  
  4. begin  
  5. open cur;  
  6. loop  
  7.     fetch cur into cursor_record;  
  8.     exit when cur%notfound;  
  9.     dbms_output.put_line(cursor_record.sno);  
  10. end loop;  
  11.     close cur;  
  12. end;  



(14)循环结构:

a) loop ...if 条件 then  exit ... end loop;


b) loop ... exit when 条件 ... end loop;

[sql] view plaincopy在CODE上查看代码片派生到我的代码片
  1. declare  
  2.   n number :=1;  
  3.   count1 number :=2;  
  4. begin  
  5.   loop  
  6.   n :=n*count1;  
  7.   count1 :=count1+1;  
  8.   exit when count1=11;  
  9.   end loop;  
  10.   dbms_output.put_line(to_char(n));  
  11. end;  

c) while 条件 loop ... end loop;


d) for ... in ...loop ...end loop;


[sql] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -- case两种用法(1)  
  2. declare  
  3.    v_job varchar2(9);  
  4. begin  
  5.    select job into v_job from emp where ename = 'KING';  
  6.    case v_job  
  7.      when 'CLERK' then dbms_output.put_line('KING IS A CLEAK');  
  8.      when 'PRESIDENT' then dbms_output.put_line('KING IS A PRESIDENT');  
  9.      when 'SALESMAN' then dbms_output.put_line('KING IS A SALESMAN');  
  10.      else dbms_output.put_line('KING is not found!');  
  11.      end case;  
  12. end;  
  13.   
  14. --case两种用法(2)  
  15. declare  
  16.    v_job varchar2(9);  
  17. begin  
  18.    select job into v_job from emp where ename = 'KING';  
  19.    case  
  20.      when  v_job='CLERK' then dbms_output.put_line('KING IS A CLEAK');  
  21.      when v_job='PRESIDENT' then dbms_output.put_line('KING IS A PRESIDENT');  
  22.      when v_job='SALESMAN' then dbms_output.put_line('KING IS A SALESMAN');  
  23.      else dbms_output.put_line('KING is not found!');  
  24.      end case;  
  25. end;  
goto用法:

[sql] view plaincopy在CODE上查看代码片派生到我的代码片
  1.   --goto语句的用法  
  2. create table temp (sno char(6),sex char(2));  
  3. declare  
  4.  v_counter binary_integer :=1;  
  5.  v_sno number(6);  
  6. begin  
  7.  v_sno :=10;  
  8.  loop  
  9.   insert into temp(sno,sex) values (to_char(v_sno),'男');  
  10.   v_counter := v_counter + 1;  
  11.   v_sno := v_sno +1;  
  12.   if v_counter =10 then  
  13.      goto loop_end;  
  14.   end if;  
  15.   end loop;  
  16.  <<loop_end>>  
  17.    dbms_output.put_line('success');  
  18. end;