Oracle基础(十三)复合变量

65 篇文章 0 订阅
64 篇文章 0 订阅

复合变量

1.%type : 指定变量的数据类型和数据库中的列一致.

1.获取一个变量的数据类型

2.%rowtype

1.获取表中一行记录

3.PL/SQL 记录(自定义类型)

一个类型可以包含多个变量

  type record_name_type is record(

  field1_name data_type [not null][default| :=] default_value.
  
  )

  record_name_type : 自定义记录(类型) 的名称
  field1_name : 字段名称
  data_type : 字段属性

标量是存储一行单列的值
复合变量一行多列

多行单列(PL/SQL集合)
PL/SQL集合类型是类似于高级语言中的数组(集合),集合类型包括
1.索引表(PL/SQL表)

1.处理PL/SQL中 的数组数据类型

1.高级语言的数组长度是有限制的,并且下标不能为负数.
2.索引表数组长度没有限制,并且下标可以为负数

  	type type_name is table of element_type
  	[not null] index by key_type;
  	
  	identifier type_name;

type_name : 自定义数据类型的名称(is table…index 表示是一个索引表)
element_type 索引表元素的数据类型
not null : 表示不能引入null元素
key_type : 索引表元素下标的数据类型(binary_integer,pls_integer或varchar2)

整形(只能为整数)
binary_integer : 2~31次方 ,如果超出存储上限,自动转换成number类型
pls_integer : 2~31次方 如果超出存储上限,抛出异常
identifier : 自定义索引表变量名称;

		-- Created on 2019-09-04 by LINNE 
		declare 
		  type dname_table_type is table of dept.dname%type -- 指定索引表的存储的数据类型
		       index by binary_integer; -- 指定索引表下标的数据类型
		   dname_table dname_table_type;
		   i number;
		begin
		  -- Test statements here
		  
		     select dname into dname_table(1) from dept where deptno = 10;
		     select dname into dname_table(2) from dept where deptno = 20;
		     select dname into dname_table(3) from dept where deptno = 30;
		     select dname into dname_table(4) from dept where deptno = 40;
		     
		    -- dbms_output.put_line(dname_table(2));
		  
		    for i in 1..4 loop
		      
		        dbms_output.put_line(dname_table(i));
		        
		    end loop;
		  
		end;


		-- Created on 2019-09-04 by LINNE 
		declare 
		  -- Local variables here
		  i integer;
		  type area_table_type is table of number
		       index by varchar2(10);
		  area_table area_table_type;
		begin
		  -- Test statements here
		  
		    area_table('c新乡') := 6;
		    area_table('b郑州') := 5;
		    area_table('a驻马店') :=30;
		    
		    dbms_output.put_line('第一个元素 : '||area_table.first);
		    dbms_output.put_line('最后一个元素 : '||area_table.last);
		    dbms_output.put_line('下一个元素 : '||area_table.next('新乡'));
		  
		  
		end;

嵌套表

  1. 嵌套表也是pl/sql数组的数据类型的
    2. 高级语言数组元素下标从0或1开始的.并且元素个数有限制
    3. 嵌套表下标必须从1开始,并且元素没有元素限制
    4. 高级语言中数组是有序的,嵌套表元素的数组可以是无序的.
    5. 索引表类型不能做为表中列的数据类型使用
    6. 嵌套表类型可以作为表中列的数据类型使用

     type type_name is table of element_type;
     			idetifer type_name;
    

type_name : 用于指定嵌套表的类型名
element_type : 嵌套表元素的数据类型
idetifer : 定义嵌套表类型变量

在使用嵌套表元素之前,必须首先使用其构造方法初始化嵌套表.

-- Created on 2019-09-04 by LINNE 
			declare 
			 type ename_table_type is table of emp.ename%type;
			 
			 ename_table ename_table_type;
			 
			begin
			  -- Test statements here
			  ename_table := ename_table_type('李四','王五','赵柳','丽丝尔');-- 使用构造方法初始化嵌套表变量
			  	
			  select ename into ename_table(2) from emp where empno = 7788;
			  
			  dbms_output.put_line(ename_table(1));
			  
			end;

表列中使用嵌套表

create type phone_type is table of varchar2(50);

		
		create table employee(
		
		       id number(4),
		       name varchar2(10),
		       phone phone_type
		
		)
		nested table phone store as phone_table;--phone_table 就是集合的表名
		
		
		
		insert into employee values(1001,'ccc',phone_type('0373-1234567','13603738558'))
		insert into employee values(1002,'bccc',phone_type('0373-1234567','13603738558','12345678'))
		
		
		
		select * from employee;

边长数组
1. 其他同上
2. 元素的最大个数是有限制的

		type type_name is varray(size) of element_type [not null];

		
		
		declare
		  type dept_count_record is record(
		    dno     emp.deptno%type,
		    v_count number); -- 自定义pl/sql记录
		
		  type dept_count_type is table of dept_count_record index by binary_integer; -- 定义索引集合表
		
		  dept_table dept_count_type; --创建索引表实例
		
		  i integer; --定义标量
		begin
		  -- 查询第一行数据赋值给索引表集合第一个元素
		  select deptno, n
		    into dept_table(1)
		    from (select rownum r, c.*
		            from (select deptno, count(1) n from emp group by deptno) c) t
		   where t.r = 1;
		
		  select deptno, n
		    into dept_table(2)
		    from (select rownum r, c.*
		            from (select deptno, count(1) n from emp group by deptno) c) t
		   where t.r = 2;
		
		  select deptno, n
		    into dept_table(3)
		    from (select rownum r, c.*
		            from (select deptno, count(1) n from emp group by deptno) c) t
		   where t.r = 3;
		
		  dbms_output.put_line(dept_table.count);
		  -- 循环集合中的所有元素
		  for i in 1 .. dept_table.count loop
		    -- 分支判断
		    dbms_output.put_line(dept_table(i)
		                         .dno || ' 总人数 : ' || dept_table(i).v_count);
		    if dept_table(i).v_count > 5 then
		      dbms_output.put_line(dept_table(i).dno || '是土豪...有' || dept_table(i)
		                           .v_count || '个人!');
		    elsif dept_table(i).v_count > 3 then
		      dbms_output.put_line(dept_table(i).dno || '是小土豪...有' || dept_table(i)
		                           .v_count || '个人!');
		    
		    else
		      dbms_output.put_line(dept_table(i)
		                           .dno || '你真穷' || dept_table(i).v_count || '个人!');
		    
		    end if;
		  
		  end loop;
		
		end;
集合方法

exists() : 检查集合中是否存在指定的下标
count() : 获取集合中元素的个数
limit() : 获取最大元素个数
first()
last()
next()
prior()

delete() : 删除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值