【问题标题】:use nested table value in where clause(oracle)在 where 子句(oracle)中使用嵌套表值
【发布时间】:2014-03-22 12:51:26
【问题描述】:

我是 Oracle DBMS 的新手,想知道如何在 where 子句中使用嵌套表的成员

特别是
我有一个名为 poll_nest 的嵌套表

   SQL> desc poll_nest
     Name                                      Null?    Type
    ----------------------------------------- -------- ----------------------------

    ID                                                 VARCHAR2(20)
    CID                                                VARCHAR2(20)

创建如下

create or replace type voter_arrive as object(id varchar(20),cid varchar(20));
create or replace type poller as table of voter_arrive;

然后作为 poll_nest 插入到选举表中

    SQL> desc election;
    Name                                      Null?    Type
   ----------------------------------------- -------- ----------------------------

     REGION                                             VARCHAR2(20)
     STIME                                              TIMESTAMP(6)
     ETIME                                              TIMESTAMP(6)
     VOTES                                              POLLER

我需要根据当前输入的 poll_nest 的 cid 属性的值采取一些措施(例如根据 cid 递增一个值)

所以我使用了after trigger,我这样做了:

select distinct t.cid into voted from election e,table(e.votes) t where t.cid=:new.votes.cid;

但我得到一个编译错误。 我也看到了this 的回答和这个: Oracle Nested Table predicate in where clause
但无法理解它们是如何工作的..

有人可以帮我了解语法吗?

【问题讨论】:

    标签: sql oracle oracle10g nested-table


    【解决方案1】:

    poll_nest 不是嵌套表。它存储 PL SQL 对象的表。

    来自http://www.orafaq.com/wiki/NESTED_TABLE

    NESTED TABLE 是一种 Oracle 数据类型,用于支持包含多值属性的列,在这种情况下,列可以容纳整个子表。

    您可以通过首先实例化对象构造函数来在对象类型表中插入值

    insert into poll_nest values (voter_arrive('122','112'));

    要访问插入的值,您可以使用

    select e.voter.arrive.cid from poll_tab e where e.voter_arrive.id = '122';

    也请查看此链接:http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/objects.htm

    更新: 我查阅了 oracle 文档 http://docs.oracle.com/cd/A97630_01/appdev.920/a96624/05_colls.htm ,发现 poller 是一个嵌套表。我不知道他们为什么称它为嵌套表,它应该只称为 PL/SQL 表。

    我假设 poll_nest 是这样的,并回答了这个问题 CREATE TABLE APPS.POLL_NEST ( VOTER APPS.VOTER_ARRIVE )

    但现在我认为您已经创建了表格和对象,例如

    create or replace type voter_arrive as object(id varchar(20),cid varchar(20));
    
    
     create or replace type poller as table of voter_arrive;
    
    create  table election(
     REGION                                             VARCHAR2(20),
     STIME                                              TIMESTAMP(6),
     ETIME                                              TIMESTAMP(6),
     VOTES                                              POLLER
    )    NESTED TABLE VOTES STORE AS VOTES_TAB;
    

    对于每个区域(应该是唯一的),您将在 VOTES 嵌套表中保存记录。如果我是正确的

    INSERT INTO election
     VALUES ( 'A',
             CURRENT_TIMESTAMP,
             CURRENT_TIMESTAMP,poller(voter_arrive('100','001'),voter_arrive('200','002')) )
    

    poller() 是嵌套表类型轮询器的构造函数。传递给构造函数的所有值都将在轮询器中创建新行。因此,您可以像

    一样在选举表中执行插入和更新
    INSERT INTO table (select e.votes from election e where region ='A')      values((voter_arrive('300','003')))
    

    要访问轮询器嵌套表中的单个行,您需要使用 TABLE() 函数,如下所示: select * from table (select e.votes fromelection e where region ='A') a where a.id = 100

    【讨论】:

    • 感谢您的回答..我尝试了您建议的语法并得到 ORA-04044: procedure, function, package, or type is not allowed here..可能是什么错误?@crazycloud ...我接受了你的回答,让我了解嵌套表
    • @craycloud 感谢您提供了非常丰富的答案,并感谢您抽出宝贵的时间!!!也很好的解释... :)
    猜你喜欢
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多