ITPub博客

首页 > 数据库 > 数据库开发技术 > PLSQL学习-【4复合数据类型】

PLSQL学习-【4复合数据类型】

原创 数据库开发技术 作者:哎呀我的天呐 时间:2014-11-21 16:06:39 1 删除 编辑

记录record:(变量存放一行值)

declare 
 type tp_emp is record(ename emp.ename%type ,empno emp.empno%type,sal emp.sal%type);
 v_tp tp_emp;
begin
  select ename,empno,sal into v_tp from emp where empno = 7788;
  dbms_output.put_line(v_tp.ename ||' '|| v_tp.empno ||' ' || v_tp.sal);
end;

rowtype:(select后必须全部列出来 )

declare 
  v_tp emp%rowtype;
  begin
    select empno,ename,job,mgr,hiredate,sal,comm,deptno into v_tp from emp where empno = 7788;
    dbms_output.put_line(v_tp.ename);
  end;


table:
索引表:

declare 
14:31:07   2 type tp_emp is table of emp.ename%type index by binary_integer;
14:39:28   3  v_tp tp_emp;
14:39:36   4 begin
14:39:38   5 select ename into v_tp(1) from emp where empno = 7788;
14:40:01   6  dbms_output.put_line(v_tp(1));
14:40:18   7 end;
14:40:21   8 /


嵌套表:(得初始化)

declare
 type tp_emp is table of emp.ename%type;
 v_tp tp_emp:=tp_emp();
begin
  v_tp.extend;
  select ename into v_tp(1) from emp where empno = 7788;
  dbms_output.put_line(v_tp(1));
end;
declare
  type tp_emp is table of emp.ename%type index by binary_integer;
  v_tp tp_emp;
  begin
    select ename into v_tp(1) from emp where empno = 7788;
    select ename into v_tp(2) from emp where empno = 7900;
    select ename into v_tp(3) from emp where empno = 7369;
    
    dbms_output.put_line(v_tp(1)||' '||v_tp(2)||' '||v_tp(3));
    dbms_output.put_line('count'|| v_tp.count);         --总计多少个角标。
    dbms_output.put_line('first'|| v_tp.first);
    dbms_output.put_line('last'|| v_tp.last);
    dbms_output.put_line('prior(3)'|| v_tp.prior(3));
    dbms_output.put_line('next(2)'|| v_tp.next(2));
    
    if v_tp.exists(3) then 
      dbms_output.put_line('exists(3)'||1);
    else 
      dbms_output.put_line('exists(3)'||);
    end if;
    
    v_tp.delete(1);
    dbms_output.put_line(v_tp.count);
    dbms_output.put_line(v_tp(2)||' '||v_tp(3));
    --v_tp.trim(1);
    dbms_output.put_line(v_tp.count);
   end;


table叫table的原因,其实是存储一列值的,但是当以一行值为元素即rowtype,就会构成table;
打印dept表:

点击( 此处)折叠或打开

  1. declare

  2. type tp_dept is table of dept %rowtype index by binary_integer ;

  3. v_tp tp_dept ;

  4. cursor cu_dept is select * from dept ;

  5. v_num number ;

  6. begin

  7.   v_num : =1 ;

  8.    for i in cu_dept loop

  9.      select * into v_tp (v_num ) from dept where deptno = i .deptno ;

  10.     v_num : =v_num +1 ;

  11.    end loop ;

  12.    for j in 1 . .v_tp . count loop

  13.     dbms_output .put_line (v_tp (j ) .deptno | | \ ' \' | | v_tp (j ) .dname | | \ ' \' | |v_tp (j ) .loc ) ;

  14.     

  15.    end loop ;

  16. end ;





可变数组:略

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29990276/viewspace-1341745/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
https://cloud.tencent.com/developer/inventory/14362

注册时间:2014-10-30

  • 博文量
    403
  • 访问量
    2528690