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

相关文章

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

Java 正则表达式的使用实战案例

《Java正则表达式的使用实战案例》本文详细介绍了Java正则表达式的使用方法,涵盖语法细节、核心类方法、高级特性及实战案例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、正则表达式语法详解1. 基础字符匹配2. 字符类([]定义)3. 量词(控制匹配次数)4. 边

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

使用Python的requests库来发送HTTP请求的操作指南

《使用Python的requests库来发送HTTP请求的操作指南》使用Python的requests库发送HTTP请求是非常简单和直观的,requests库提供了丰富的API,可以发送各种类型的HT... 目录前言1. 安装 requests 库2. 发送 GET 请求3. 发送 POST 请求4. 发送

Nginx中配置使用非默认80端口进行服务的完整指南

《Nginx中配置使用非默认80端口进行服务的完整指南》在实际生产环境中,我们经常需要将Nginx配置在其他端口上运行,本文将详细介绍如何在Nginx中配置使用非默认端口进行服务,希望对大家有所帮助... 目录一、为什么需要使用非默认端口二、配置Nginx使用非默认端口的基本方法2.1 修改listen指令

Python WebSockets 库从基础到实战使用举例

《PythonWebSockets库从基础到实战使用举例》WebSocket是一种全双工、持久化的网络通信协议,适用于需要低延迟的应用,如实时聊天、股票行情推送、在线协作、多人游戏等,本文给大家介... 目录1. 引言2. 为什么使用 WebSocket?3. 安装 WebSockets 库4. 使用 We

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十