Home » SQL & PL/SQL » SQL & PL/SQL » Collections- Associative arrays (Oracle, 11.2.0.4.0, Windows 7 Professional)
Collections- Associative arrays [message #646520] Sun, 03 January 2016 17:19 Go to next message
buggleboy007
Messages: 282
Registered: November 2010
Location: Canada
Senior Member
First off, I am weak in Collections topic. Presently understanding Associative arrays and facing a glitch. Hope someone can help me out.

I am trying to retrieve only "employee first names" from HR schema table being Employees. Here's what I have done to retrieve only employee first names
CREATE OR REPLACE PACKAGE HR.xtractempdetails 
IS

    TYPE empspecificfirstname_rt IS TABLE OF VARCHAR2(32767) INDEX BY BINARY_INTEGER;--
    -- TYPE empspecificfirstname_rt IS TABLE OF EMPLOYEES.first_name%TYPE INDEX BY BINARY_INTEGER;
     empspecificfname_aa     empspecificfirstname_rt;


-- Public Procedures

/***********************************************************************
NAME      :   getempdetails
TYPE      :   PROCEDURE
INPUT     :   Employee id
OUTPUT    :   All employee details for a specific employee id delcared 
              in empsepecificdetails_rt(empsepecificdetails_aa)
DESCRIPTION : This procedure gets all employee details from HR.employees
              table for all employees


************************************************************************/

      
PROCEDURE getempdetails(p_InNumempid        IN EMPLOYEES.employee_id%TYPE,
                        p_Outtypeempfname   OUT empspecificfirstname_rt
                        );
                        --p_Outtypeemplname   OUT empspecificlastname_rt,
                       -- p_Outtypeempemailid OUT empspecificemail_rt);      
END xtractempdetails;
/



Package specification compiles successfully. Now comes the package body

CREATE OR REPLACE PACKAGE BODY HR.xtractempdetails
IS

PROCEDURE getempdetails(p_InNumempid        IN EMPLOYEES.employee_id%TYPE,
                        p_Outtypeempfname   OUT empspecificfirstname_rt
                        )
                        
IS

BEGIN

         SELECT first_name
         BULK COLLECT INTO  p_Outtypeempfname 
          FROM  employees 
          WHERE employee_id=p_InNumempid;
          
        FOR idx IN p_Outtypeempfname.FIRST..p_Outtypeempfname.LAST
        LOOP
            p_Outtypeempfname(idx).first_name;
        
        END LOOP;       
          


END xtractempdetails;
/


This is not compiling and gives me the following errors:

a)PLS-00487: Invalid reference to variable "VARCHAR2"
(This is probably because of the variable: TYPE empspecificfirstname_rt IS TABLE OF VARCHAR2(32767) INDEX BY BINARY_INTEGER; that I am using)

b)PLS-00487:Invalid reference to variable "Employees.FIRST_NAME%TYPE"
(This happens when I comment TYPE empspecificfirstname_rt IS TABLE OF VARCHAR2(32767) INDEX BY BINARY_INTEGER
and use TYPE empspecificfirstname_rt IS TABLE OF EMPLOYEES.first_name%TYPE INDEX BY BINARY_INTEGER;)

What could be the issue? Can any one help me out?

Thanks,
BB

Re: Collections- Associative arrays [message #646521 is a reply to message #646520] Sun, 03 January 2016 17:32 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
WHY?
What is benefit from duplicating data which already resides in table into collection?
What problem are you trying to solve?
How will we know when correct solution is posted here?

Do you realize & understand that PL/SQL is entirely separate & distinct language from SQL?
What is valid in one language may not be valid in the other language.

>TYPE empspecificfirstname_rt
is above a PL/SQL object or SQL object?

Can SQL use PL/SQL object?
Re: Collections- Associative arrays [message #646522 is a reply to message #646521] Sun, 03 January 2016 17:37 Go to previous messageGo to next message
buggleboy007
Messages: 282
Registered: November 2010
Location: Canada
Senior Member
BlackSwan wrote on Sun, 03 January 2016 17:32
WHY?
What is benefit from duplicating data which already resides in table into collection?
What problem are you trying to solve?
How will we know when correct solution is posted here?

Do you realize & understand that PL/SQL is entirely separate & distinct language from SQL?
What is valid in one language may not be valid in the other language.

>TYPE empspecificfirstname_rt
is above a PL/SQL object or SQL object?

Can SQL use PL/SQL object?


I am just using the above code for practice and understanding sake. I am not sure what you mean by duplicating data which already resides in table into collection. Yes, I realise and understand that PL/SQL is separate and distinct from SQL.

The type that I have mentioned is part of the package specs which is clearly defined.

I don't know what you are trying to ask or your intention behind asking the above questions.
Re: Collections- Associative arrays [message #646523 is a reply to message #646522] Sun, 03 January 2016 17:46 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
https://docs.oracle.com/database/121/LNPLS/toc.htm

above shows valid syntax for every PL/SQL construct

https://www.google.com/webhp?hl=en&tab=ww#hl=en&q=pl%2Fsql+collection+example
Re: Collections- Associative arrays [message #646524 is a reply to message #646523] Sun, 03 January 2016 17:51 Go to previous messageGo to next message
buggleboy007
Messages: 282
Registered: November 2010
Location: Canada
Senior Member
Any one else other than BS that can help me out? I am basically trying to accomplish my task using BULK COLLECT with a SELECT statement instead of a CURSOR.
Re: Collections- Associative arrays [message #646525 is a reply to message #646524] Sun, 03 January 2016 20:33 Go to previous messageGo to next message
buggleboy007
Messages: 282
Registered: November 2010
Location: Canada
Senior Member
This issue is now addressed. Don't bother replying anymore
Re: Collections- Associative arrays [message #646526 is a reply to message #646525] Sun, 03 January 2016 20:42 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
12. If you found an answer yourself, post it. That way we know the issue is resolved and we might learn from it.
Re: Collections- Associative arrays [message #646527 is a reply to message #646526] Sun, 03 January 2016 23:45 Go to previous messageGo to next message
buggleboy007
Messages: 282
Registered: November 2010
Location: Canada
Senior Member
BlackSwan wrote on Sun, 03 January 2016 20:42
12. If you found an answer yourself, post it. That way we know the issue is resolved and we might learn from it.


What is 12 BS? In the quest to put me down, you write whatever you want. Anyways the answer is:

employee number being unique and all I want is emp details, I should have used a for loop (For I in (Select co1,co2,co3 from table_name)) to retrieve my results rather than a collection. In the quest to gain more hands on with collections, I used what I did. I realize the better approach towards this could have been: a) Passing a department number and fetching all employees belonging to a specific department. This would have been a better approach to gain more hands on.
Re: Collections- Associative arrays [message #646534 is a reply to message #646527] Mon, 04 January 2016 01:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
What is 12 BS


OraFAQ Forum Guide

Re: Collections- Associative arrays [message #646557 is a reply to message #646534] Mon, 04 January 2016 07:42 Go to previous messageGo to next message
buggleboy007
Messages: 282
Registered: November 2010
Location: Canada
Senior Member
Michel Cadot wrote on Mon, 04 January 2016 01:58

Quote:
What is 12 BS


OraFAQ Forum Guide



MC: I think you too have pinged me without reading BS's message. When BS sent me a message that says "12. If you found an answer yourself, post it. That way we know the issue is resolved and we might learn from it."

I wanted to know what that 12 was and this was addressed to BS clearly. Anyways doesn't matter. It may be a typo but one needs to pay attention to what they type or rather proof read before submitting their reply.
Re: Collections- Associative arrays [message #646558 is a reply to message #646557] Mon, 04 January 2016 08:01 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
The 12 is the number of a rule in the forum guide. BS did a direct copy/paste. It would have helped if he'd added some context.
Re: Collections- Associative arrays [message #646559 is a reply to message #646557] Mon, 04 January 2016 08:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Did you bother to read the link I posted?
No, you did not, did you?
You should have read it hundred times these last 5 years and this would prevent you from posting your last 2 messages.

Re: Collections- Associative arrays [message #646560 is a reply to message #646558] Mon, 04 January 2016 08:06 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
cookiemonster wrote on Mon, 04 January 2016 15:01
.. It would have helped if he'd added some context.


Seems not necessary for me when addressed to someone who has registered 5 years ago. The context is implicit. Wink

Re: Collections- Associative arrays [message #646569 is a reply to message #646558] Mon, 04 January 2016 09:00 Go to previous messageGo to next message
buggleboy007
Messages: 282
Registered: November 2010
Location: Canada
Senior Member
cookiemonster wrote on Mon, 04 January 2016 08:01
The 12 is the number of a rule in the forum guide. BS did a direct copy/paste. It would have helped if he'd added some context.


Absolutely. I concur with you CM about what you have said. If Mr.Cadot had just added the sentence "12 refers to one of the pointers on ORAFAQ guide" it would have helped me. Just posting a one liner or copy paste does not help at times. Not every one is as sharp and outspoken (blatantly has a barb tongue) as Mr.Cadot.

Anyways Thanks Mr.Cadot for scolding me (which is what you do when others cannot follow you). Your point noted.
Re: Collections- Associative arrays [message #646570 is a reply to message #646569] Mon, 04 January 2016 09:49 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I don't understand the problem, a simple click would pop up the answer you asked.
Sorry to have thought you're smarter than you are, I didn't mean to insult you with my implicit answer.
Your point is noted.

[Updated on: Mon, 04 January 2016 09:50]

Report message to a moderator

Previous Topic: Getting ORA-01839: date not valid for month specified
Next Topic: XML parsing error-ORA-06512
Goto Forum:
  


Current Time: Fri Apr 26 05:49:23 CDT 2024