oracle的自定义类型

可用用关键字create type 或者直接用type定义自定义类型,

区别是 用 create 后面用 as , 若直接用 type 后面用 is,另 type用在语句块中,而create 是的独立的.

declare   

   type t_table is table of varchar2(10) index by BINARY_integer;   

   MyTab   t_table;   

begin  

   MyTab(1) :=  'A';   

   MyTab(2) :=  'B';   

   MyTab(3) :=  'C';   

   DBMS_OUTPUT.PUT_LINE('First index:'||'   '|| mytab(1) ||'   ');   

end;  

declare 

  o o_air := (1,'test');

begin

  select 1 from dual; 

end;

/

自定义类型一般分为两中,object类型和table类型.object类似与一个recored可以表示一个表的一行数据,

object的字段就相当与表的字段.

自定义的table类型需要用的已经定义好的object类型.

一般定义object的语法:

用 

create type 自定义表类型A as table of 自定义Object类型A 

和 

create type 自定义Object类型A as object(

字段1 类型1,

字段2 类型2

);

与 

type 自定义表类型B is table of 类型 

和 

type 自定义Object类型B is record(

字段1 类型1,

字段2 类型2

);

实例:

(1)定义对象类型:TYPE sales_country_t

CREATE TYPE sales_country_t AS OBJECT (

   YEAR              VARCHAR2 (4),

   country           CHAR (2),

   sum_amount_sold   NUMBER

);

(2)定义表类型:TYPE SUM_SALES_COUNTRY_T_TAB

CREATE TYPE sum_sales_country_t_tab AS TABLE OF sales_country_t;

(3)定义对象类型:TYPE sales_gender_t

CREATE TYPE sales_gender_t AS OBJECT (

   YEAR              VARCHAR2 (4),

   country_id        CHAR (2),

   cust_gender       CHAR (1),

   sum_amount_sold   NUMBER

);

(4)定义表类型:TYPE SUM_SALES_GENDER_T_TAB

CREATE TYPE sum_sales_gender_t_tab AS TABLE OF sales_gender_t;

(5) 也可以使用基本类型定义  表类型比如:

create or replace type test_tab_type as table of varchar2(4000);

/

varchar2(4000) 是一个资本类型,这样相当于定义的表中只有一个字段varchar2(4000)

用法:

TYPE sales_country_t_rec IS RECORD (

      YEAR              VARCHAR (4),

      country           CHAR (2),

      sum_amount_sold   NUMBER

   );

v_sales_country_t_rec sales_country_t_rec;

引用: 

v_les_country_t_rec.year := 'ssss'; 

v_sales_country_t_rec.country := 'a'; 

v_sales_country_t_rec.sum_amount_sold := 2 ;

----------------------------------------------------------

1 首先创建一个数据类型

create type t_air  as object(id int ,name varchar(20));

2 创建表

create table aaa(

        id int ,person t_air);

3 插入数据

insert into aaa values(1,t_air(1,'23sdf'));

4 查询classPlace

select a.id ,a.persion.id,a.person.name from aaa a;

t_air(1,'23sdf') 使用这个方式创建一个自定义类型t_air的对象.

------------------------------------------------------------

关于 index by

declare   

   type t_table is table of varchar2(10) index by BINARY_integer;   

   MyTab   t_table;   

begin  

   MyTab(1) :=  'A';   

   MyTab(2) :=  'B';   

   MyTab(3) :=  'C';   

   DBMS_OUTPUT.PUT_LINE('First index:'||'   '|| mytab(1) ||'   ');   

end;  

详细参考:http://blog.csdn.net/feimashenhua/article/details/7177948

--------------------------------------------------------------------

oracle自定义函数是最重要的函数之一,下面就为您介绍oracle自定义函数的使用,实现返回表类型,希望对您能够有所帮助

oracle中的函数可以返回表类型。但是,这个表类型实际上是集合类型(与数组类似)这个类型不能直接作为 from 的宾语。

  

从oracle 9i 开始,提供了一个叫做“管道化表函数”的概念,来解决这个问题。

这种类型的函数,必须返回一个集合类型,且标明 pipelined。

这个函数不能返回具体变量,必须以一个空 return 返回。

这个函数中,通过 pipe row () 语句来送出要返回的表中的每一行。

  

调用这个函数的时候,通过 table() 关键字把管道流仿真为一个数据集 

以下是一个十分简单的实例:

create table tb1(k number, v varchar2(10));  

insert into tb1(k, v) values(100,'aaa');  

insert into tb1(k, v) values(200,'bbb');  

insert into tb1(k, v) values(200,'ccc');  

select * from tb1;  

create type row_type1 as object(k number, v varchar2(10));  

create type table_type1 as table of row_type1;  

create or replace function fun1 return table_type1 pipelined as  

v row_type1;  

begin  

for myrow in (select k, v from tb1) loop  

  v := row_type1(myrow.k, myrow.v);  

  pipe row (v);  

end loop;  

return;  

end;  

select * from table(fun1);

---------------------------------------------------

oracle 表类型变量的使用 

http://blog.sina.com.cn/s/blog_8e5087d101014a1l.html

使用记录类型变量只能保存一行数据,这限制了SELECT语句的返回行数,如果SELECT语句返回多行就会错。Oracle提供了另外

一种自定义类型,也就是表类型,它是对记录类型的扩展,允许处理多行数据,类似于表。

创建表类型的语法如下:

TYPE table_name IS TABLE OF data_type [ NOT NULL ]

INDEX BY BINARY_INTEGER ;

语法说明如下:

--table_name  创建的表类型名称。

--IS TABLE  表示创建的是表类型。

--data_type  可以是任何合法的PL/SQL数据类型,例如varchar2。

--INDEX BY BINARY_INTEGER  指定系统创建一个主键索引,用于引用表类型变量中的特定行。

使用表类型的例子:

 例一:直接对表类型变量赋值

declare

type my_emp is table of scott.emp%rowtype

index by binary_integer;

new_emp my_emp;

begin

new_emp(1).empno:=6800;

new_emp(1).ename:='tracy';

new_emp(1).job:='clerk';

new_emp(1).sal:=2500;

new_emp(2).empno:=6900;

new_emp(2).ename:='luck';

new_emp(2).job:='manager';

new_emp(2).sal:=4000;

dbms_output.put_line(new_emp(1).empno ||','||new_emp(1).ename||','||new_emp(1).job||

','||new_emp(1).sal);

dbms_output.put_line(new_emp(2).empno ||','||new_emp(2).ename||','||new_emp(2).job||

','||new_emp(2).sal);

end;

/

例二:使用表类型变量的方法:变量名.方法名[索引号]

declare

type my_emp is table of scott.emp%rowtype

index by binary_integer;

new_emp my_emp;

begin

new_emp(1).empno:=6800;

new_emp(1).ename:='tracy';

new_emp(1).job:='clerk';

new_emp(1).sal:=2500;

new_emp(2).empno:=6900;

new_emp(2).ename:='luck';

new_emp(2).job:='manager';

new_emp(2).sal:=4000;

dbms_output.put_line(new_emp.first||','||new_emp.count||','||new_emp.last);

dbms_output.put_line(new_emp(1).empno ||','||new_emp(1).ename||','||new_emp(1).job||

','||new_emp(1).sal);

dbms_output.put_line(new_emp(2).empno ||','||new_emp(2).ename||','||new_emp(2).job||

','||new_emp(2).sal);

--new_emp.delete(1);

dbms_output.put_line(new_emp.next(1));

end;

//

例三:与游标结合使用

declare

type my_emp is table of scott.emp%rowtype

index by binary_integer;

new_emp my_emp;

v_num number:=0;

cursor cur_emp is select empno,ename,job,sal from scott.emp;

begin

for v_emp in cur_emp loop

v_num:=v_num+1;

select * into new_emp(v_num) from scott.emp

where ename=v_emp.ename;

end loop;

for i in 1..new_emp.count loop

dbms_output.put_line(new_emp(i).empno ||','||new_emp(i).ename||','||new_emp(i).job||

','||new_emp(i).sal);

end loop;

end;

/

注意:不能直接对表变量赋值:select * into new_emp from scott.emp where deptno=30; 这种赋值方法是错的,赋值需要

使用下标,如上面的的例子。

猜你喜欢

转载自jackyin5918.iteye.com/blog/1985195