---
title: "oracle数据库PL/SQL学习的例子"
author: "lengyue"
date: "2011-08-30T23:04:19Z"
tags: []
categories: ["数据库"]
slug: "oracle-pl-sql"
draft: false
lastmod: "2011-08-30T23:04:19Z"
id: 1161912
url: "https://lengyue.iteye.com/blog/1161912"
---

oracle记录的学习.  
声明一个记录  
begin  
 declare  
 type myrecord is record(  
 id number,  
 user\_type varchar(20)  
 );

 realrecord myrecord;  
 begin  
   select id,user\_type into realrecord from base\_user where user\_name='zhangjiaxun';

   DBMS\_OUTPUT.PUT\_LINE(realrecord.id||','||realrecord.user\_type);  
 end;  
 

end;

声明一个表的记录类型.

begin  
 declare  
 realrecord  base\_user%rowtype;

 begin  
   select * into realrecord from base\_user where user\_name='zhangjiaxun';

   DBMS\_OUTPUT.PUT\_LINE(realrecord.id||','||realrecord.user\_type);  
 end;  
 

end;

以一个表的字段声明一个记录类型.  
begin  
 declare  
 type myrecord is record  
 (  
   id base\_user.id%type,  
   user\_type base\_user.user\_type%type  
 );

 realrecord myrecord;  
 begin  
   select id,user\_type into realrecord from base\_user where user\_name='zhangjiaxun';

   DBMS\_OUTPUT.PUT\_LINE(to\_char(realrecord.id)||','||realrecord.user\_type);  
 end;  
 

end;

游标的学习

begin  
  declare  
  srecord S%ROWTYPE;  
  currentrow number;  
  cursor scursor is select s1,s2 from S ;   
  begin  
  open scursor;  
  fetch scursor into srecord;

  while scursor%FOUND loop  
     currentrow := scursor%rowcount;  
     if(currentrow mod 2=0) then  
        begin  
        insert into a(a1,a2)values(srecord.s1,srecord.s2);  
        commit;  
        exception  
        when others then         
        rollback;  
        end;         
        DBMS\_OUTPUT.PUT\_LINE('偶数');  
     else  
        begin  
        insert into b(b1,b2)values(srecord.s1,srecord.s2);  
        commit;  
        exception  
        when others then  
        rollback;  
        end;  
        DBMS\_OUTPUT.PUT\_LINE('奇数');  
     end if;  
     fetch scursor into srecord;  
  end loop;  
  close scursor;  
  exception  
  when others then  
  begin  
    if scursor%isopen then  
     close scursor;  
  end if;  
     raise;  
  end;  
  end;  
   
end;

for循环游标.

begin  
  declare  
  srecord S%ROWTYPE;  
  currentrow number;  
  cursor scursor is select s1,s2 from S ;   
  begin

    for srecord in scursor loop  
     currentrow := scursor%rowcount;  
     if(currentrow mod 2=0) then  
        begin  
        insert into a(a1,a2)values(srecord.s1,srecord.s2);  
        commit;  
        exception  
        when others then         
        rollback;  
        end;         
        DBMS\_OUTPUT.PUT\_LINE('偶数');  
     else  
        begin  
        insert into b(b1,b2)values(srecord.s1,srecord.s2);  
        commit;  
        exception  
        when others then  
        rollback;  
        end;  
        DBMS\_OUTPUT.PUT\_LINE('奇数');  
     end if;  
     end loop;  
  exception  
  when others then  
  begin  
    if scursor%isopen then  
     close scursor;  
  end if;  
     raise;  
  end;  
  end;  
   
end;