Oracle type (自定义类型的使用)

2024-09-09 05:18

本文主要是介绍Oracle type (自定义类型的使用),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

oracle - type

 

type定义:

oracle中自定义数据类型
oracle中有基本的数据类型,如number,varchar2,date,numeric,float....但有时候我们需要特殊的格式,

如将name定义为(firstname,lastname)的形式,我们想把这个作为一个表的一列看待,这时候就要我们自己定义一个数据类型
格式 :create or replace type type_name as object(parameter1 varchar2(20),parameter2 varchar2(30));
这样子定义之后我们就能像使用基本类型一样使用自定义类型去建表:
如:
create or replace type name_format as object(firstname varchar2(20),lastname varchar2(30));
定义了类型之后,我们就可以将它当作普通的基本类型用了,建表,写function等等,如用自定义数据类型建表:
create table testtable(id number primary key,name name_format);

插入数据时,构造我们的这个自定义列时,就像构造对象一样,如上面的表插入数据的sql语句如下:
insert into testtable values(1,name_format('hust','lkq'));
查询自定义列的某个值可以用.来访问,如上面我要查询testtable表中name列的firstname的值可以按照如下查询:
select t.name.firstname from testtable t;

自定义数据类型也可以是集合
例如:create or replace type numers_set as array(10) of number;
这就表示创建了一个包含10个number数据的自定义类型,
注:这里的集合里面的基本元素也可以是你自定义的类型,也就是说支持嵌套定义
create or replace type name_list as array(10) of name_format:(此处的name_format就是我们在上面定义的一个自定义类型

 

 将表中查出来的值赋值给ob:

复制代码
select rec_planratemain(planrgroupmainid,pl.productid,planrateversion,pl.createuserid,pl.createdate,pl.updateuserid,pl.updatedate,ratedatetype,startdate,enddate,p.branchid,channeltype)into g_rec_planmainrate -- 实例化后的名字from planrgroupmain pl,productbranch_property pwhere pl.productid = 'PROG0000003001024'and p.productid = pl.productid --and startdate <= p_date--and ENDDATE >= p_dateand p.branchid = 'BRA0000000000011'and rownum < 2;
复制代码

 

OB:

 

 NT:

 

 

调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function  getagentbroker(p_branchid  IN  VARCHAR2,
                           p_month     in  VARCHAR2,
                           p_agentcode in  varchar2,
                           p_type        in  varchar2) return  nt_test is
     v_Count   SMALLINT  DEFAULT  0;
     v_NT_test NT_test;
     v_num     smallint  default  10;
   begin
      if p_type = 'B'   then
         v_num  := 0;
      end  if;    
     v_NT_test := NEW nt_test();
   
     for  c_ag in  ( select  a.agentid,
                         a.recommendagentid,
                         getagentstatus(a.agentstatus) agentstatus,
                         a.agentcode,
                         a.agentname,
                         a.agentlevelcode,
                         a.hiredate
                    from  agenthis a
                   where  a.bizyearmonth = p_month
                     and  a.agentstatus = 'AGENTSTATUS_09'
                     and  a.branchid = p_branchid
                     and  (a.agentcode =p_agentcode or  p_agentcode is  null )
                     and  (rownum <= v_num or  v_num = 0)
                  
                  ) loop
     
       v_Count := v_Count + 1;
       v_NT_test.Extend;
       v_NT_test(v_Count) := ob_test;
     
       v_NT_test(v_Count).ca01 := c_ag.agentname;
       v_NT_test(v_Count).ca02 := c_ag.agentcode;
       v_NT_test(v_Count).ca03 := c_ag.agentlevelcode;
       v_NT_test(v_Count).ca04 := c_ag.agentstatus;
       v_NT_test(v_Count).ca05 := c_ag.hiredate;
     
       ----一代管理人
       if c_ag.recommendagentid is  not  null  then
         begin
           select  agentcode,
                  agentname,
                  getagentstatus(agentstatus) agentstatus,
                  agentlevelcode,
                  recommendagentid
             into  v_NT_test(v_Count).ca06,
                  v_NT_test(v_Count).ca07,
                  v_NT_test(v_Count).ca08,
                  v_NT_test(v_Count).ca09,
                  v_NT_test(v_Count).ca10
             from  agenthis
            where  bizyearmonth = '201203'
                 -- and agentstatus='AGENTSTATUS_09'
              and  agentid = c_ag.recommendagentid;
         exception
           when  others then
             v_NT_test(v_Count).ca10 := '' ;
         end ;
         ----2代管理人
         if v_NT_test(v_Count)
          .ca10 != ''  or  v_NT_test(v_Count).ca10 is  not  null  then
           begin
             select  agentcode,
                    agentname,
                    getagentstatus(agentstatus) agentstatus,
                    agentlevelcode,
                    recommendagentid
               into  v_NT_test(v_Count).ca11,
                    v_NT_test(v_Count).ca12,
                    v_NT_test(v_Count).ca13,
                    v_NT_test(v_Count).ca14,
                    v_NT_test(v_Count).ca15
               from  agenthis
              where  bizyearmonth = '201203'
                   -- and agentstatus='AGENTSTATUS_09'
                and  agentid = v_NT_test(v_Count).ca10;
           exception
             when  others then
               v_NT_test(v_Count).ca15 := '' ;
           end ;
           ----3代管理人
         
           if v_NT_test(v_Count)
            .ca15 != ''  or  v_NT_test(v_Count).ca15 is  not  null  then
             begin
               select  agentcode,
                      agentname,
                     getagentstatus(agentstatus) agentstatus,
                      agentlevelcode,
                      recommendagentid
                 into  v_NT_test(v_Count).ca16,
                      v_NT_test(v_Count).ca17,
                      v_NT_test(v_Count).ca18,
                      v_NT_test(v_Count).ca19,
                      v_NT_test(v_Count).ca20
                 from  agenthis
                where  bizyearmonth = '201203'
                     --  and agentstatus='AGENTSTATUS_09'
                  and  agentid = v_NT_test(v_Count).ca15;
             exception
               when  others then
                 v_NT_test(v_Count).ca20 := '' ;
             end ;
  
           end  if;
         end  if;
       end  if;
     end  loop;
     return  v_NT_test;
   end ;

 Type:

复制代码
 1 CREATE OR REPLACE TYPE "OB_SMSSEND"                                                                          AS OBJECT (
 2     SMSSENDID          VARCHAR2(64),     -- N ??????
 3     CREATEUSERID          VARCHAR2(32),     -- Y ????
 4     CREATEDATE          DATE,     -- N ????
 5     UPDATEUSERID          VARCHAR2(32),     -- Y ????
 6     UPDATEDATE          DATE,     -- N ????
 7     BIZNO          VARCHAR2(32),     -- Y ??????
 8     SMSBIZCODE          VARCHAR2(16),     -- Y ??????
 9     SMSTEXTDESC          VARCHAR2(1024),     -- Y ????
10     RECVMOBILENO          VARCHAR2(32),     -- Y ????
11     APPROVESTATUS          VARCHAR2(32),     -- Y ??????
12     SENDSTATUS          VARCHAR2(32),     -- Y ??????
13     SENDDATE          DATE,     -- Y ????
14     RECVCUSTOMERID          VARCHAR2(64),     -- Y ????
15     RECVBACK          VARCHAR2(64),     -- Y ??????
16     RECVBACKDATE          DATE,     -- Y ????
17     CONSTRUCTOR FUNCTION OB_SMSSEND RETURN SELF AS RESULT
18 );
复制代码
一步一个脚印,方便自己复习,该出手时就出手,有错误,一定要指正,非常感谢,共同进步!

转自:http://www.cnblogs.com/o-andy-o/archive/2012/05/25/2517741.html

这篇关于Oracle type (自定义类型的使用)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1150264

相关文章

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图