当前位置:数据库 > Oracle >>

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
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,