Home  >  Article  >  Database  >  Oracle %type和%rowtype小实例

Oracle %type和%rowtype小实例

WBOY
WBOYOriginal
2016-06-07 17:00:521082browse

//%type //如果声明的变量是直接映射到数据库的某一列上,那么就可以使用%type关键字将变量 //锚定到这个列上。这样做有什么

Oracle %type和%rowtype小实例

[日期:2011-04-28] 来源:Linux社区  作者:Linux [字体:]

//%type  
//如果声明的变量是直接映射到数据库的某一列上,那么就可以使用%type关键字将变量  
//锚定到这个列上。这样做有什么好处呢?  
//比如:  
//declare v_ename scott.emp.ename%type;  
//当数据类型发生变化时,此方法显得非常灵活。  
//如果更改了列的长度,那么锚定到该列上的所有变量都会自动更改其长度;  
//假设我们将v_ename定义为varchar2(10),那么当emp表中的ename列发生变化时,  
//我们得手动将v_enam更改为emp.ename相同的数据长度;  
//当我们使用锚定类型后,,变量就会自动进行调整。  
//%rowtype  
//%rowtype与%type相似;不过它将变量锚定到表的所有列,而不是锚定到某一列;  
//更多关于%rowtype与%type,  
//请参考:  
//下面是一个实例:  
create table dept(  
       deptno varchar2(5),  
       dname varchar2(20),  
       loc varchar2(20)  
       );  
create or replace procedure pro_insert(  
       deptno_in in dept.deptno%type,  
       dname_in in dept.dname%type,  
       loc_in in dept.loc%type  
       )  
as 
  v_dept dept%rowtype;  
begin  
     begin  
          insert into dept  
          select deptno_in,dname_in,loc_in  
          from dual;  
          commit;  
          dbms_output.put_line('inserting successed');  
          exception  
          when others then  
               rollback;  
     end;  
     begin  
          select deptno_in,dname_in,loc_in  
          into v_dept from dual;  
          dbms_output.put_line(  
          'The data having been inserted.'||  
          'deptno:'||v_dept.deptno||  
          ',dname:'||v_dept.dname||  
          ',loc:'||v_dept.loc  
          );  
     end;  
end pro_insert;  
//  
//上面的过程中,使用到了嵌套块;  
//所谓嵌套块就是块中包含其他子块;  
//嵌套块允许出现在代码块的异常处理部分和执行部分,  
//但是不允许出现在声明中。  
//  
SQL> set serveroutput on;  
SQL> exec pro_insert('111','财务部','福州');  
inserting successed  
The data having been inserted.deptno:111,dname:财务部,loc:福州  
PL/SQL procedure successfully completed  
//  
//从这里看出来,%rowtype定义的变量作用有点相似游标,  
//但是我们不能将%rowtype定义的变量当做游标来使用,  
//否则我们将会得到下面的错误:  
//ORA-01422: exact fetch returns more than requested number of rows  
declare v_dept dept%rowtype;  
begin  
     select deptno,dname,loc  
     into v_dept  
     from dept;  
     dbms_output.put_line(  
          'The data having been inserted.'||  
          'deptno:'||v_dept.deptno||  
          ',dname:'||v_dept.dname||  
          ',loc:'||v_dept.loc  
          );  
end;  
//  
//下面我们用游标来实现上面的操作,具体看下面的匿名块:  
declare  
       cursor cv_dept is 
       select *  
       from dept;  
begin  
     //变量v_dept不必我们显示声明  
     //for循环会为我们隐式的打开和关闭游标,不必我们现实的打开和关闭游标  
     for v_dept in cv_dept loop  
     dbms_output.put_line(  
          'deptno:'||v_dept.deptno||  
          ',dname:'||v_dept.dname||  
          ',loc:'||v_dept.loc  
          );  
      end loop;  
end;  
//  
deptno:111,dname:财务部,loc:福州  
deptno:120,dname:销售部,loc:大连  
deptno:130,dname:科研部,loc:北京  
PL/SQL procedure successfully completed 

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn