oracle 的rowtype使用

oracle 中%ROWTYPE的使用

1:table_name%ROWTYPE
    declare
   v_t1      t1%rowtype;
 begin
   select tid,tname into v_t1 from t1 where tid = 1;
  
   dbms_output.put_line(v_t1.tid || ' -- '||v_t1.tname);
 end;

2: 使用游标
 declare
   cursor cur is select tid,tname from t1 where 1 = 2;
   v_t1   cur%ROWTYPE;
 begin
   select tid,tname into v_t1 from t1 where tid = 1;
  
   dbms_output.put_line(v_t1.tid || ' -- '||v_t1.tname);
 end;
 
3   通过record
    declare
   type rec_t1 is record(
   tid     t1.tid%type,
   tname   t1.tname%type
   );
   v_t1   rec_t1;
 begin
   select tid,tname into v_t1 from t1 where tid = 1;
  
   dbms_output.put_line(v_t1.tid || ' -- '||v_t1.tname);
 end;

4: record嵌套rowtype
 declare
   type rec_t1 is record(
   tb_t1     t1%rowtype
   );
   v_t1   rec_t1;
 begin
   select tid,tname into v_t1.tb_t1 from t1 where tid = 1;
  
   dbms_output.put_line(v_t1.tb_t1.tid || ' -- '||v_t1.tb_t1.tname);
 end;
 
1: 一般用于需要一个表全部字段时使用

2,3,4:一般在返回一个表部分字段或多表查询返回多个表的字段时使用

你可能感兴趣的:(oracle,开发)