Thursday, May 27, 2010

Selecting Column from Oracle User Defined Type (Nested Table)

this is how to select a single column from a multi-column nested table.
CREATE OR REPLACE TYPE question_obj AS OBJECT
(QUESTIONSEQ VARCHAR2(10),
QUESTION_LABEL VARCHAR2 (200 BYTE),
TYPESEQ NUMBER (2),
COMPULSORY NUMBER (1),
MAX_ANSWER NUMBER (2),
GROUPSEQ NUMBER (2),
ORDER_NO NUMBER (2))
/


CREATE OR REPLACE TYPE question_tbl is table of question_obj;
/



DECLARE
l_questions question_tbl;
BEGIN
-- this following statement would populate the nested table with data
-- from a table
SELECT question_obj (q.questionseq,
q.question_label,
q.typeseq,
q.compulsory,
q.max_answer,
q.groupseq,
q.order_no)
BULK COLLECT
INTO l_questions
FROM QUESTION q
WHERE q.surveyseq = '2010050195';

-- now let's demonstrate how to select from a nested table
FOR r IN (SELECT q.questionseq
FROM TABLE (l_questions) q
WHERE q.questionseq IS NOT NULL)
LOOP
DBMS_OUTPUT.put_line (r.questionseq);
END LOOP;
END;

Note: You can NOT use sql againts PLSQL table, that is you declare the type in PLSQL declaration or package specification

No comments: