(转)oracle - type

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

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 p
         where pl.productid = ‘PROG0000003001024‘
         and p.productid = pl.productid
              --and startdate <= p_date
              --and ENDDATE >= p_date
           and p.branchid = ‘BRA0000000000011‘
           and rownum < 2;

OB:

CREATE OR REPLACE TYPE "OB_TEST"    AS OBJECT (
  ca01          VARCHAR2(64),
  ca02          VARCHAR2(32),
  ca03          VARCHAR2(32),
  ca04          VARCHAR2(32),
  ca05          VARCHAR2(32),
  ca06          VARCHAR2(32),
  ca07          VARCHAR2(32),
  ca08          VARCHAR2(32)
  CONSTRUCTOR FUNCTION OB_TEST RETURN SELF AS RESULT
); 

NT:

CREATE OR REPLACE TYPE "NT_TEST" is table of OB_TEST;

调用:

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:

CREATE OR REPLACE TYPE "OB_SMSSEND"                                                                          AS OBJECT (
    SMSSENDID          VARCHAR2(64),     -- N ??????
    CREATEUSERID          VARCHAR2(32),     -- Y ????
    CREATEDATE          DATE,     -- N ????
    UPDATEUSERID          VARCHAR2(32),     -- Y ????
    UPDATEDATE          DATE,     -- N ????
    BIZNO          VARCHAR2(32),     -- Y ??????
    SMSBIZCODE          VARCHAR2(16),     -- Y ??????
    SMSTEXTDESC          VARCHAR2(1024),     -- Y ????
    RECVMOBILENO          VARCHAR2(32),     -- Y ????
    APPROVESTATUS          VARCHAR2(32),     -- Y ??????
    SENDSTATUS          VARCHAR2(32),     -- Y ??????
    SENDDATE          DATE,     -- Y ????
    RECVCUSTOMERID          VARCHAR2(64),     -- Y ????
    RECVBACK          VARCHAR2(64),     -- Y ??????
    RECVBACKDATE          DATE,     -- Y ????
    CONSTRUCTOR FUNCTION OB_SMSSEND RETURN SELF AS RESULT
);

一步一个脚印,方便自己复习,该出手时就出手,有错误,一定要指正,非常感谢,共同进步!

时间: 2024-08-24 00:43:39

(转)oracle - type的相关文章

ORACLE %TYPE与%ROWTYPE

如果要定义一个类型与某个变量的数据类型或数据库表中某个列的数据类型一致的变量的变量, 可以使用%TYPE来实现. 如果要定义一个与数据库中某个表结构一致的记录类型的变量,可以使用%ROWTYPE. 以默认测试用户scott下的表做例子 注:安装的时候如果没解锁可以用sys登录,执行解锁语句: alter user scott account unlock; 1 DECLARE 2 v_ename emp.ename % TYPE; 3 v_emp emp % ROWTYPE; 4 BEGIN 5

Oracle连接池操作

一:首先是将JDBC的驱动包oracle6.jar复制到Tomcat安装路径下的llib目录下 二:在web工程目录下的META-INF\context.xml文件下输入以下代码(特别注意文件夹别搞错了): <span style="font-size:14px;color:#663366;"><?xml version="1.0" encoding="UTF-8"?> <Context debug="5&

C++完成Oracle存储过程批量插入(一)

为了满足大数据量的批量插入这个需求,最近研究了一下Oracle数据库的批量插入这块儿,本篇先介绍一下这两天来所了解到的以及一次不成功的C++进行存储过程批量插入方案. 一.Oracle处理批量插入存储过程 1.Oracle处理批量插入涉及到的相关概念有:Type.bulk collect.Oracle数组等.现在对它们依次简单的介绍一下. 1)Type Type主要用于用户建立Oracle自定义类型. Type常用的自定义类型介绍: A.子类型 类似与全局定义的常量,只要改一处地方,与此常量相关

oracle配置与server.xml配置

* 1,java web工程可以部署到tomcat上,也可以通过配置server.xml映射到工程实际目录,这样在tomcat的webapps目录下就没有工程了,下面是server.xml中的配置 <Context path="/myfirst" docBase="E:\myJava\project2\first\WebRoot" debug="0" reloadable="false"> <Resource

oracle 自动诊断信息库(Automatic Diagnostic Repository,ADR)

ORACLE 11G中的ADR介绍: ADRCI 之oracle 日志查看 在Oracle的11g版本中,alert文件的位置发生了变化,与此同时Oracle提供了一个命令行工具ADRCI,它可以很便利的查看数据库中出现的异常. 以下给大家展示一下使用ADRCI (ADR Command Interface) 在11g中查看Oracle的警告日志的方法. 1.进入到adrci命令行模式[email protected] /home/oracle$ which adrci/oracle/u01/a

Web Api 2, Oracle and Entity Framework

Web Api 2, Oracle and Entity Framework I spent about two days trying to figure out how to expose the Oracle.ManagedDataAccess 4.121.1.0 library over a Web Api 2.2 OData v4 Endpoint. Looking through the Oracle documentation, Oracle Managed Data Access

使用Oracle 11g的adrci的ips打包一个incident

[[email protected] ~]$ which adrci /u02/app/oracle/product/11.2.0.4/db_1/bin/adrci [[email protected] ~]$ adrci ADRCI: Release 11.2.0.4.0 - Production on Sat May 2 22:32:28 2015----->这是os时间 Copyright (c) 1982, 2011, Oracle and/or its affiliates. All

BizTalk 2013R2 WCF-LOB Oracle Adapter安装配置/问题&解决方法

BizTalk 2013R2 WCF-LOB Oracle Adapter安装配置/问题&解决方法 安装Oracle Adapter 安装Oracle客户端 BizTalk 2013R2 安装包自带的WCF LOB Adapter Pack for Oracle 需要Oracle客户端组件 ODP.NET或ODAC组件.这里很容易出现问题,版本的不兼容,因为Oracle Adapter默认需要安装 11.1.0.7这个版本 The Oracle Database adapter uses the

Oracle数据库用EF操作的示例

Using EF Oracle Sample Provider with EDM Designer  (from msdn) Many people are asking if it is possible to use EFOracleProvider with EDM Designer in Visual Studio 2008 SP1. The answer is yes, but because the sample doesn't include a DDEX provider req