How can you add complex (Object) element to "Nested Table"?
542526Apr 14 2009 — edited Apr 14 2009hello I've done some reading on *"Associative Array"* and *"Nested Table"* and would like to enumerate through the collection using the later (http://www.devshed.com/c/a/Oracle/Database-Interaction-with-PLSQL-Nested-Tables/2/)
- i.e. enumerate "Nested Table"
However, ran into problem trying to *"EXTEND"* my nested table (add elements of complex type or OBJECT I defined, namely "TmpHierarchyMapObjType" below) - detail as follows:
-- NOTE 1: Here's how I create the object type (Be nice if I can just say "CREATE OR REPLACE TYPE TmpHierarchyMapObjType AS OBJECT (_TMP_HIERACHICAL.ROWTYPE_)" instead of having to duplicate column definition.
CREATE OR REPLACE TYPE TmpHierarchyMapObjType AS OBJECT
(
Id numeric(19,0) ,
ParentId numeric(19,0),
ChildId numeric(19,0),
...
TmpUID varchar2(32),
ReferencedId numeric(19,0),
...
);
-- NOTE 2: I can't put "TmpHierarchyMapObjType" inside the package but that's not crucial I suppose.
CREATE OR REPLACE PACKAGE types
AS
TYPE cursorType IS REF CURSOR;
TYPE TmpHiearchyMapTableType IS TABLE OF TmpHierarchyMapObjType; /* Use "Nested Table" instead of Associative Array */
/* TYPE TmpHiearchyMapTableType IS TABLE OF TMP_HIERARCHYMAP%ROWTYPE INDEX BY BINARY_INTEGER; */
END;
-- NOTE 3: Here's my TABLE FUNCTION which now returns a "Nested Table" as supposed to "Associative Array" (with different enumeration syntax)
CREATE OR REPLACE FUNCTION spGetParentsTable
(
ObjectId number,
ObjectClassifier varchar2
)
RETURN types.TmpHiearchyMapTableType -- Now an "Nested Table", not "Associative Array"
IS
TmpHierarchyMap types.TmpHiearchyMapTableType;
ThisTempId varchar2(32);
CURSOR spGetParents_cursor IS
SELECT
ReferencedId Id,
ParentId,
ChildId,
...
FROM TMP_HIERARCHYMAP
WHERE TmpUID = ThisTempId;
BEGIN
SELECT sys_guid() INTO ThisTempId FROM dual;
spRecursiveGetParents(ObjectId, ObjectClassifier, ThisTempId);
-- (Commented out) TmpHierarchyMapMAX := 0;
FOR oMap in spGetParents_cursor LOOP
-- (Commented out) TmpHierarchyMapMAX := TmpHierarchyMapMAX + 1;
-- QUESTION: FAILED here! How can I add complex types/elements?
TmpHierarchyMap.EXTEND(cast(oMap as TmpHierarchyMapObjType));
/*
NOTE: (Commented out) Not using "Associative Array", now using "Nested table" instead.
TmpHierarchyMap(TmpHierarchyMapMAX).Id := oMap.Id;
TmpHierarchyMap(TmpHierarchyMapMAX).ParentId := oMap.ParentId;
TmpHierarchyMap(TmpHierarchyMapMAX).ChildId := oMap.ChildId;
...
*/
END LOOP;
DELETE FROM TMP_HIERARCHYMAP WHERE TmpUID = ThisTempId;
RETURN TmpHierarchyMap;
END spGetParentsTable;
-- Lastly, I want to enumerate nested table.
DECLARE
oMappingTable types.TmpHiearchyMapTableType;
BEGIN
oMappingTable := spGetParentsTable(2,'ThinkFundamentals.Util.Security.SystemUser');
-- NOTE 4: This is my end-goal!
select * from table(cast(oMappingtable as types.TmpHiearchyMapTableType));
dbms_output.put_line('done!');
END;
REF:
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/collections.htm#LNPLS005
http://www.devshed.com/c/a/Oracle/Associative-Arrays-in-Oracle-PLSQL-Introduction/2/
http://www.devshed.com/c/a/Oracle/Database-Interaction-with-PLSQL-Nested-Tables/2/