Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

problem with CAST MULTISET

Posted on 2009-05-11
3
Medium Priority
?
1,141 Views
Last Modified: 2013-12-18
I'm learning how to use CAST MULTISET.  The following resulted in an error ("ORA-00904 : Invalid identifier" for the syntax "candidates.experiences").  What would be the correct way to do this?
CREATE OR REPLACE TYPE t_experience AS OBJECT
(
    companyname    varchar2(20),
    position       varchar2(20),
    noofyears      number(2)
);
/
 
CREATE OR REPLACE TYPE t_experience_tbl AS TABLE OF t_experience; 
/
 
CREATE TABLE candidates
(
    name            varchar2(20),
    experiences     t_experience_tbl
) NESTED TABLE experiences STORE AS experiences_tab;
/
 
INSERT INTO candidates VALUES
(
    'John',
    t_experience_tbl
    (
        t_experience('A company','Software Engineer',3),
        t_experience('B company','System Analyst',2),
        t_experience('C company','Research Fellow',4)
     )
);
/
 
INSERT INTO candidates VALUES
(
    'Jack',
    t_experience_tbl
    (
        t_experience('E company','Hardware Engineer',3),
        t_experience('F company','Data Analyst',5),
        t_experience('G company','Database Admin',5)
     )
);
/
 
DECLARE
  l_tempTable t_experience_tbl;
 
BEGIN
  SELECT experiences INTO l_tempTable FROM candidates WHERE name = 'John';
  SELECT experiences MULTISET UNION l_tempTable INTO l_tempTable FROM candidates WHERE name = 'Jack';
 
  FOR i IN l_tempTable.FIRST .. l_tempTable.LAST LOOP
    DBMS_OUTPUT.PUT_LINE(l_tempTable(i).companyname || ' ' || l_tempTable(i).position || ' ' || l_tempTable(i).noofyears);
  END LOOP;
 
  -- everything works fine up to this point --
  -- then I added this part to play with CAST MULTISET
  -- to see if I can get it to do the work of the few lines of code above
  
  SELECT CAST (MULTISET (SELECT * FROM TABLE(candidates.experiences)) AS t_experience_tbl) INTO l_tempTable FROM dual;
 
  FOR i IN l_tempTable.FIRST .. l_tempTable.LAST LOOP
    DBMS_OUTPUT.PUT_LINE(l_tempTable(i).companyname || ' ' || l_tempTable(i).position || ' ' || l_tempTable(i).noofyears);
  END LOOP;
END;
/

Open in new window

0
Comment
Question by:n_fortynine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
Andytw earned 2000 total points
ID: 24412200
If you want to use CAST( MULTISET(  ) ) then see the code below:
DECLARE
   l_tempTable t_experience_tbl;
 
BEGIN
   SELECT experiences
   INTO   l_tempTable
   FROM   candidates
   WHERE  NAME = 'John';
   SELECT experiences MULTISET
   UNION l_tempTable
   INTO   l_tempTable
   FROM   candidates
   WHERE  NAME = 'Jack';
 
   FOR i IN l_tempTable.FIRST .. l_tempTable.LAST LOOP
      DBMS_OUTPUT.PUT_LINE(l_tempTable(i).companyname || ' ' || l_tempTable(i).position || ' ' || l_tempTable(i).noofyears);
   END LOOP;
 
   -- everything works fine up to this point --
   -- then I added this part to play with CAST MULTISET
   -- to see if I can get it to do the work of the few lines of code above
 
   /*SELECT CAST(MULTISET (SELECT *
                FROM   TABLE(candidates.experiences)) AS t_experience_tbl)
   INTO   l_tempTable
   FROM   dual;*/
   SELECT CAST(MULTISET (SELECT e.companyname,
                    e.position,
                    e.noofyears
             FROM   candidates c,
                    TABLE(c.experiences) e
             WHERE  c.NAME IN ('John', 'Jack')) AS t_experience_tbl) experiences
   into l_tempTable          
   FROM   dual;
   
 
   FOR i IN l_tempTable.FIRST .. l_tempTable.LAST LOOP
      DBMS_OUTPUT.PUT_LINE(l_tempTable(i).companyname || ' ' || l_tempTable(i).position || ' ' || l_tempTable(i).noofyears);
   END LOOP;
END;
/

Open in new window

0
 
LVL 11

Assisted Solution

by:Andytw
Andytw earned 2000 total points
ID: 24412222
However, I think the following is a lot more readable:

SELECT t_experience(e.companyname, e.position, e.noofyears)
BULK COLLECT INTO l_tempTable
FROM   candidates c,
             TABLE(c.experiences) e
WHERE  c.NAME IN ('John', 'Jack');
0
 
LVL 4

Author Comment

by:n_fortynine
ID: 24427720
Ah, thanks for pointing out the correct syntax.  If you don't mind me asking, what is the purpose of CAST MULTISET then, when BULK COLLECT will work just as well?  (Should I make a separate question?)
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many‚Ķ
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question