Question

i am fairly new to the Oracle DBMS and would like to know how to use a member of a nested table in a where clause

specifically
i have a nested table named poll_nest

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

    ID                                                 VARCHAR2(20)
    CID                                                VARCHAR2(20)

which was created as follows

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

and then it was inserted into the election table as poll_nest

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

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

i need to take some actions based on the value of the cid attribute of poll_nest that is currently entered(like incrementing a value based on the cid)

so i used an after trigger in which i did this:

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

but i get a compilation error. i saw this answer and this too: Oracle Nested Table predicate in where clause
but couldn't understand how they work..

could someone please help me with the syntax?

Was it helpful?

Solution

poll_nest is not a nested table. Its table that stores PL SQL object.

From http://www.orafaq.com/wiki/NESTED_TABLE :

NESTED TABLE is an Oracle data type used to support columns containing multivalued attributes, in this case, columns that can hold an entire sub-table.

You can insert value in table of object type by first instantiating object constructor like

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

For accessing the inserted value you can use

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

check out this link too: http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/objects.htm

Update: I looked up oracle documentation http://docs.oracle.com/cd/A97630_01/appdev.920/a96624/05_colls.htm , and I found that poller is a nested table. I don't know why they call it nested table, it should be called PL/SQL table only.

I assumed poll_nest to be like, and answered the question CREATE TABLE APPS.POLL_NEST ( VOTER APPS.VOTER_ARRIVE )

but now I think you have created table and objects like

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;

For each region(which should be unique), you are going to save records in VOTES nested table .if I am correct

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

poller() is a constructor of nested table type poller. All values passed to constructor will create new row in poller. So, you can perform insert and update in election table like

INSERT INTO table (select e.votes from election e where region ='A')      values((voter_arrive('300','003')))

To access individual row in poller nested table you need to use TABLE() function like this: select * from table (select e.votes from election e where region ='A') a where a.id = 100

election table after insert

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top