how do I CREATE IF NOT EXISTS Temp table in PLSQL?
542526Apr 21 2009 — edited Apr 21 2009hello, how do I CREATE IF NOT EXISTS Temp table in PLSQL? The following table is to be created in FIRST call inside a recursive function (which you'll see in QUESTION 2).
QUESTION 1:
CREATE GLOBAL TEMPORARY TABLE TmpHierarchyMap
(
Id numeric(19,0) NOT NULL,
ParentId numeric(19,0) NOT NULL,
ChildId numeric(19,0) NOT NULL,
... more ...
) on commit delete rows');
QUESTION 2: How to return a temp table from a function?
For example, this is how I'm doing it at the moment, using Nested Table.
EXECUTE IMMEDIATE 'CREATE OR REPLACE TYPE TmpHierarchyMapObjType AS OBJECT
(
Id numeric(19,0) ,
ParentId numeric(19,0),
ChildId numeric(19,0),
... more ...
); ';
EXECUTE IMMEDIATE 'CREATE OR REPLACE TYPE TmpHierarchyMapTableType AS TABLE OF TmpHierarchyMapObjType;';
CREATE OR REPLACE FUNCTION fnGetParentsTable
(
ObjectId number,
ObjectClassifier varchar2
)
RETURN TmpHierarchyMapTableType
IS
TmpHierarchyMap TmpHierarchyMapTableType := TmpHierarchyMapTableType();
ThisTempId varchar2(32);
CURSOR spGetParents_cursor IS
SELECT
Id,
ParentId,
ChildId,
...
FROM TMP_HIERARCHYMAP
WHERE TempId = ThisTempId;
BEGIN
SELECT sys_guid() INTO ThisTempId FROM dual;
spRecursiveGetParents(ObjectId, ObjectClassifier, ThisTempId);
FOR oMap in spGetParents_cursor LOOP
TmpHierarchyMap.Extend();
TmpHierarchyMap(TmpHierarchyMap.Count) := TmpHierarchyMapObjType( oMap.Id
, oMap.ParentId
, oMap.ChildId
...
);
END LOOP;
DELETE FROM TMP_HIERARCHYMAP WHERE TempId = ThisTempId;
RETURN TmpHierarchyMap;
END fnGetParentsTable;
QUESTION 3: what does the word GLOBAL means? I read that temp table is visible only to a particular database connection/session and will be dropped automatically on termination of the session. i can only find this information in some forum discussion but failed to locate this in Oracle doc, can someone point me in right direction please?
Many thanks!
REF:
http://stackoverflow.com/questions/221822/sybase-developer-asks-how-to-create-a-temporary-table-in-oracle
http://www.oracle-base.com/articles/8i/TemporaryTables.php