首页 > Linux操作系统 > Linux操作系统 > %rowtype是什么?
我直接给出一个%rowtype的例子很多学员说理解起来有困难!
declare
cursor c1 is select * from t_emp ;
v_emp t_emp%rowtype ;
begin
open c1 ;
fetch c1 into v_emp ;
while not c1%notfound loop
dbms_output.put_line(
v_emp.id||' '||v_emp.name||' '||
v_emp.sex||' '||v_emp.age||' '||v_emp.salary||' '||
v_emp.birthday||' '||v_emp.depart_code) ;
fetch c1 into v_emp ;
end loop ;
close c1 ;
end;
--===================================
于是我尝试通过下面的代码解释一下,效果很好!但是oracle内部是不是通过这样实现的我不得而知!
declare
cursor c1 is select * from t_emp ;
type v_emp_type is record(
id t_emp.id%type,
name t_emp.name%type,
sex t_emp.sex%type,
age t_emp.age%type,
salary t_emp.salary%type,
birthday t_emp.birthday%type,
depart_code t_emp.depart_code%type
) ;
v_emp v_emp_type ;
begin
open c1 ;
fetch c1 into v_emp ;
while not c1%notfound loop
dbms_output.put_line(
v_emp.id||' '||v_emp.name||' '||
v_emp.sex||' '||v_emp.age||' '||v_emp.salary||' '||
v_emp.birthday||' '||v_emp.depart_code) ;
fetch c1 into v_emp ;
end loop ;
close c1 ;
end;
--=======================================
试想如果没有%rowtype,那么定义太多的变量将成为一件很繁琐的事情!
declare
cursor c1 is select * from t_emp ;
v_id int;
v_name varchar2(20) ;
v_sex varchar2(10) ;
v_age int ;
v_salary number ;
v_birthday date ;
v_depart_code varchar2(10) ;
begin
open c1 ;
fetch c1 into v_id , v_name , v_sex , v_age,
v_salary , v_birthday , v_depart_code ;
while not c1%notfound loop
dbms_output.put_line(c1%rowcount) ;
dbms_output.put_line(v_id||' '||v_name||' '||
v_sex||' '||v_age||' '||v_salary||' '||
v_birthday||' '||v_depart_code) ;
fetch c1 into v_id , v_name , v_sex , v_age,
v_salary , v_birthday , v_depart_code ;
end loop ;
close c1 ;
end;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/19602/viewspace-61752/,如需转载,请注明出处,否则将追究法律责任。