type和create type
type和create type
异同点:
create type 可在库中生成一个长期有效的自定义类型对象,而type作用域仅限于语句块中;
两者都可以自定义数据类型;
各种type实例:
--【create type】*****************************************************************
--①简单 type _object--------------------------------------
create or replace type test_type01 as object(
a varchar2(20),
b varchar2(20),
c number(10)); --②简单 type _varray--------------------------------------
create or replace type test_type02 as varray(10) of test_type01; --③简单 type _table--------------------------------------
create or replace type test_type03 as table of test_type02; --④创建带映射函数的【object】类型
--create type as object(member) --------------------------------------
create or replace type test_type01 as object (
a varchar2(10),
b varchar2(10),
member function aaa(a in varchar2,b in varchar2) return varchar2
);
create or replace type body test_type01 as
member function aaa(a in varchar2,b in varchar2) return varchar2 is
begin
return a||b;
end;
end;
--用法一:create table of type
create table test_type_table01 of test_type01;
insert into test_type_table01 values('a','a');
insert into test_type_table01 select test_type01('','') from dual;
commit; select * from test_type_table01;
select ee.a,ee.aaa('dd','ee') from test_type_table01 ee; --用法二:create table(typename.type)
create table test_type_table02(
a number(10),
test_t test_type01
);
insert into test_type_table02 values(1,test_type01('a','a'));
insert into test_type_table02 select 2,test_type01('b','b') from dual;
commit; select * from test_type_table02;
select rr.a,rr.test_t,rr.test_t.a,rr.test_t.aaa('dd','ee') from test_type_table02 rr; --⑤创建【table】类型
--create type as table --------------------------------------
--用法一:
create or replace type test_type02 as object (
a varchar2(10),
b varchar2(10)
) not final;
create or replace type test_type03 as table of test_type02; create table test_type_table03(
id number(10),
test_ty test_type03
)nested table test_ty store as test_ty_table;--test_ty_table表示嵌套表【名称可随意取?】; insert into test_type_table03 values(1,test_type03(test_type02('a','a')));
insert into test_type_table03 select 2,test_type03(test_type02('b','b'),test_type02('c','c')) from dual;
commit; select * from test_type_table03;
select aa.id,aa.test_ty from test_type_table03 aa;
select * from table(select aa.test_ty from test_type_table03 aa where aa.id=2);
/* --无效sql
select * from table(select aa.test_ty from test_type_table03 aa where aa.id in(1,2));--ORA-01427: 单行子查询返回多个行
select * from test_ty_table;--ORA-22812: 无法参考嵌套表列的存储表
truncate table test_type_table03;--truncate 无效,因为其中有集合
*/ --用法二:
create or replace type test_type04 as table of varchar2(10);--varchar2要限定字符数,否则会报错
create or replace type test_type05 as object(
a number(10),
test_ test_type04
);
create table test_type_table02 of test_type05
nested table test_ store as test_ty_table02;--test_为【table】类型的变量 /*--在用法二下这种定义表的语句不被允许,是因为test_type05为object?
create table test_type_table04(
id number(10),
test_t test_type05
)nested table test_ store as test_ty_table02;
*/ insert into test_type_table02 values(test_type05(1,test_type04('a')));
insert into test_type_table02 select test_type05(2,test_type04('b','c')) from dual;
commit; drop table test_type_table02;
drop type test_type04;
drop type test_type05; select * from test_type_table02;
select * from table(select tt.test_ from test_type_table02 tt where tt.a=2);
select aa.a,aa.test_ from test_type_table02 aa; --⑥创建【varray】类型
--create type as varray --------------------------------------
create or replace type test_type07 as varray(4) of varchar2(10); create table test_type_table03(
id number(10),
test_ test_type07
); insert into test_type_table03 values(1,test_type07('a'));
insert into test_type_table03
select 2,test_type07('b','c') from dual
union all
select 3,test_type07('d','e','f') from dual;
commit;
select * from test_type_table03;
select kk.* from table(select tt.test_ from test_type_table03 tt where id=3) kk; --⑦创建混合自定义类型
create or replace type test_type08 as varray(4) of varchar2(10);
create or replace type test_type09 as table of varchar2(10);--varchar2要限定字符数,否则会报错
create or replace type test_type10 as object(
a number(10),
test_ test_type08,
test_1 test_type09
); create table test_type_table05 of test_type10
nested table test_1 store as test_ty_table05;
insert into test_type_table05 values(1,test_type08('a'),test_type09('a'));
insert into test_type_table05 select 2,test_type08('b','c'),test_type09('b') from dual;
--注意以下两种写法,效果相同
insert into test_type_table05 select test_type10(3,test_type08('b','c','d','e'),test_type09('c','d')) from dual;
insert into test_type_table05 select 4,test_type08('b','c','d','e'),test_type09('c','d') from dual; select * from test_type_table05;
select * from table(select tt.test_ from test_type_table05 tt where tt.a=3);
select * from table(select tt.test_1 from test_type_table05 tt where tt.a=3); --【type】*****************************************************************
--①简单 type _object--------------------------------------
declare type test_type_ is record(
a emp.empno%type,
b emp.ename%type,
c emp.job%type);
test_ test_type_;
begin
test_.a:=123;
test_.b:='abc';
test_.c:='def';
dbms_output.put_line(test_.a||test_.b||test_.c);
select empno,ename,job into test_ from emp where empno='';
dbms_output.put_line(test_.a||test_.b||test_.c);
end;
--②简单 type _varray--------------------------------------
declare
type test_type_02 is varray(5) of varchar2(10);
test_ test_type_02;
begin
test_:=test_type_02('a','b','c','d');
dbms_output.put_line(test_(1)||test_(2)||test_(3)||test_(4));
end; --③简单 type _table--------------------------------------
declare
type test_type_03 is table of emp%rowtype;
test_ test_type_03;
begin
select empno,ename,job,mgr,hiredate,sal,comm,deptno bulk collect into test_ from emp where empno='';
for i in test_.first..test_.last loop
dbms_output.put_line(test_(i).empno||test_(i).ename||test_(i).job);
end loop;
end; --【type ref cursor】*****************************************************************
--①强类型和弱类型ref游标--------------------------------------
declare
--弱类型ref游标
type test_type is ref cursor;
--强类型ref游标(有return返回值)
type test_type_ is ref cursor return emp%rowtype;
test_ test_type;
test__ test_type_;
emp_ emp%rowtype;
emp__ emp%rowtype;
begin
open test_ for select * from emp;
loop
fetch test_ into emp_;
dbms_output.put_line(emp_.ename);
exit when test_%notfound;
end loop;
close test_;
dbms_output.put_line('##############################');
open test__ for select * from emp;
loop
fetch test__ into emp__;
dbms_output.put_line(emp__.ename);
exit when test__%notfound;
end loop;
close test__;
end; --②将ref游标作为返回值--------------------------------------
--创建函数(返回游标)
create or replace function test_func_ref return sys_refcursor as
test_ref sys_refcursor;
begin
open test_ref for select * from emp;
return test_ref;
end;
--调用函数
declare a sys_refcursor;
b emp%rowtype;
begin
a:=test_func_ref;
loop
fetch a into b;
exit when a%notfound;
dbms_output.put_line(b.empno);
end loop;
end; --sys_refcursor是9i后系统定义的一个refcursor,用于过程中返回结果集
type和create type的更多相关文章
- 转载:oracle 自定义类型 type / create type
标签:type create oracle object record 一:Oracle中的类型有很多种,主要可以分为以下几类: 1.字符串类型.如:char.nchar.varchar2.nvarc ...
- oracle 自定义类型 type / create type
一:Oracle中的类型有很多种,主要可以分为以下几类: 1.字符串类型.如:char.nchar.varchar2.nvarchar2. 2.数值类型.如:int.number(p,s).integ ...
- 基础篇之 Create Type
Create Type 的话呢,是创建一个自定义的数据类型,等于说为常用的数据类型建造一个别名的样纸.然后就可以通用当前数据库的当前架构.(当然了,一般来说我们都是使用dbo架构,所以都会无事前面那个 ...
- CREATE TYPE - 定义一个新的数据类型
SYNOPSIS CREATE TYPE name AS ( attribute_name data_type [, ... ] ) CREATE TYPE name ( INPUT = input_ ...
- Create Collection Type with Class Type Constraints
Create Collection Type with Class Type Constraints: new TypeToken<ArrayList<ClassType>>( ...
- Type.MakeGenericType 方法 (Type[]) 泛型反射
替代由当前泛型类型定义的类型参数组成的类型数组的元素,并返回表示结果构造类型的 Type 对象. 命名空间: System程序集: mscorlib(mscorlib.dll 中) public ...
- input[type='submit']input[type='button']button等按钮在低版本的IE下面,去掉黑色边框的问题
今天做一个tabs效果的时候,发现上面的button在低版本下会出现黑色的边框,很难看,于是我整理了下几个去掉黑色边框的办法: 1.在button的外层嵌套一个div,设置button的border: ...
- #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)宏的运行机理:1. ( (TYPE *)0 ) 将零转型为TY ...
- There is no result type defined for type 'json' mapped with name 'success'. Did you mean 'json'?
错误信息: 严重: Exception starting filter struts2 Unable to load configuration. - action - file:/C:/Users/ ...
随机推荐
- [HDOJ 5155] Harry And Magic Box
题目链接:HDOJ - 5155 题目大意 有一个 n * m 的棋盘,已知每行每列都至少有一个棋子,求可能有多少种不同的棋子分布情况.答案对一个大素数取模. 题目分析 算法1: 使用容斥原理与递推. ...
- 为Ubuntu配置ssh服务 方便远程登陆
Ubuntu系统必须开启ssh服务后,XP或者其他的主机才可以远程登陆到Ubuntu系统. 1,安装软件包,执行sudo apt-get install openssh-server Ubuntu缺省 ...
- stm32 smartcard调试--不用st8024
关于stm32 smartcard功能调试,官方提供的例程是配合8024芯片进行控制的.程序可从地址:http://www.pudn.com/downloads420/sourcecode/embed ...
- 线性链表的双向链表——java实现
.线性表链式存储结构:将采用一组地址的任意的存储单元存放线性表中的数据元素. 链表又可分为: 单链表:每个节点只保留一个引用,该引用指向当前节点的下一个节点,没有引用指向头结点,尾节点的next引用为 ...
- 转:Mysql读写分离实现的三种方式
1 程序修改mysql操作类可以参考PHP实现的Mysql读写分离,阿权开始的本项目,以php程序解决此需求.优点:直接和数据库通信,简单快捷的读写分离和随机的方式实现的负载均衡,权限独立分配缺点:自 ...
- 5451 HDU Best Solver
链接: Best Solver 题目分析: 这个题目的关键点是需知道“共轭”. 如 :(A√B + C√D) 和 (A√B - C√D) 是共轭的 这个有一个规律 (A√B + C√D)^n + ( ...
- (转载)eclipse 快捷键大全,eclipse查找类,文件,添加注释
(转载)http://hi.baidu.com/fegro/item/8224c8c28b174627ee466598 /* ----------------------------------- ...
- ink_test
- 使用MockMvc编写spring boot的controller的测试用例
springboot自带测试模块. 注解需要: @SpringApplicationConfiguration(classes = ComputeServiceApplication.class) 这 ...
- 《A First Course in Probability》-chaper4-离散型随机变量-随机变量函数的期望
给定一个离散型随机变量X,根据定义我们容易得到期望E[X],但是在具体的问题当中,我们会得到一个关于X的另一个函数关系Y=g(X),那么我们就非常的好奇,根据函数关系Y=g(X)和随机变量X的分布列数 ...