Collections- Associative arrays [message #646520] |
Sun, 03 January 2016 17:19 |
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 #646522 is a reply to message #646521] |
Sun, 03 January 2016 17:37 |
buggleboy007
Messages: 282 Registered: November 2010 Location: Canada
|
Senior Member |
|
|
BlackSwan wrote on Sun, 03 January 2016 17:32WHY?
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 #646527 is a reply to message #646526] |
Sun, 03 January 2016 23:45 |
buggleboy007
Messages: 282 Registered: November 2010 Location: Canada
|
Senior Member |
|
|
BlackSwan wrote on Sun, 03 January 2016 20:4212. 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 #646557 is a reply to message #646534] |
Mon, 04 January 2016 07:42 |
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 #646569 is a reply to message #646558] |
Mon, 04 January 2016 09:00 |
buggleboy007
Messages: 282 Registered: November 2010 Location: Canada
|
Senior Member |
|
|
cookiemonster wrote on Mon, 04 January 2016 08:01The 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.
|
|
|
|