PL/SQL(三):复合数据类型

一、复合数据类型
 存放多个字段,创建后可以多次使用
二、分类
 记录 表 嵌套表 数组

三、简介
1、记录
 存储一组多个字段的相关数据项,是字段的集合,主要用于从表中取出查询到的的行数据

      特殊的记录:%rowtype
      声明的表量对应数据库表或视图中列的集合,获取的是单条信息
  优点:
      对应数据库中列的数量和类型,并保持一致
2、表类型
  1) 类似于数组类型,由主键和列组成
  2) 主键:binary_integer ,列:标量或记录类型
  3) 没有长度限制,可以动态增长
  4) 表方法:
      exists(n):判断PL/SQL表中指定的元素是否存在,不能直接输出
      count : 返回一个PL/SQL表当前包含的元素的数量
      first/last:返回第一个或最后一个索引数字
      delete(n) :删除第 n 个元素
      delete(n,m) :删除从n到m中的所有元素

四.示例

1.record

declare
  type test_table is table of varchar(20) index by binary_integer;
  tt test_table;

begin
  tt(1):='ab';
  tt(2):='bb';
  tt(3):='cc';
  tt(-1):='ss';
  tt('-11'):='gk';

  dbms_output.put_line(tt.count);  -- 5
  dbms_output.put_line(tt.first);  -- -11
  dbms_output.put_line(tt.last);    -- 3
  if(tt.exists(0)) then
    dbms_output.put_line('存在');
  else
    dbms_output.put_line('不存在');    -- 
  end if;  
  
  tt.delete(1,3);  -- [1,3]
  dbms_output.put_line(tt.count);   -- 2
  tt.delete(1);   --删除下标为 1 的
  tt.delete();     --删除所有
end;

2 %rowtype

declare
  score_record T_SCORE%rowtype;
begin
  select * into score_record
  from T_SCORE where AUTOID = 10000000;  
  insert into TEST_GK values score_record; 
end;


3 %rowtype + record

declare
  type score_stu_rec is record(
    score_row T_SCORE%rowtype,
    stu_row T_STU%rowtype 
  );
  rec score_stu_rec;
  
begin
  select * into rec.score_row
  from T_SCORE ts
  where ts.exam_score = (select max(exam_score) from T_SCORE);
  
  select * into rec.stu_row
  from T_STU tstu
  where tstu.stu_id = rec.score_row.STU_ID;
  dbms_output.put_line(rec.stu_row.STU_NAME);
    
end;



















猜你喜欢

转载自blog.csdn.net/S031302306/article/details/73291843