Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Fetch cursor with a variable column number

728959Oct 20 2009 — edited Oct 21 2009
Hello guys,

this is the first time I write to this forum, as far as I remember. hence, if this is not the right place to ask this question, please point me to the right resource.

I have the following Procedure (the function split splits a string into an array):

...
BEGIN
/* SPLIT IN GROUPS AND BUILD THE PARAMETER STRINGS */
tokens_in := string_fnc.split(in_groups, '.');

for i in 1..tokens_in.count loop
IDX_REM := i;
sql_par1 := sql_par1 || ', GRP' || i || '.NAME "GROUP_' || i || '"';
sql_par2 := sql_par2 || ', DEV_XCSA.WFA_GROUP GRP' || i;
IF i = 1 THEN
sql_par3 := sql_par3 || ' AND S.ID = GRP1.FK_PARENT_SEC_ID ';
ELSE
sql_par3 := sql_par3 || ' AND GRP' || i || '.FK_PARENT_GROUP_ID ( + ) = GRP' || (i-1) || '.ID';
END IF;
end loop;
sql_par3 := sql_par3 || ' AND SGQ.FK_GROUP_ID ( + ) = GRP' || IDX_REM || '.ID';

/* BUILD THE QUERY STRING */
sql_stmt := 'SELECT A.NAME "APPRAISAL" , AQ.NAME "PROJECT" , S.NAME "SECTION"';
sql_stmt := sql_stmt || sql_par1;
sql_stmt := sql_stmt || ', SGQ.NAME "QUESTION" , SGQ.VALUE "ANSWER" FROM DEV_XCSA.WFA_APPRAISAL A, DEV_XCSA.WFA_QUESTIONNAIRE AQ, DEV_XCSA.WFA_SECTION S';
sql_stmt := sql_stmt || sql_par2;
sql_stmt := sql_stmt || ', DEV_XCSA.WFA_QUESTION SGQ WHERE A.CPHID = ''' ||USER_NAME || ''' AND A.ID = AQ.FK_APPRAISAL_ID AND AQ.ID = S.FK_QUESTIONNAIRE_ID';
sql_stmt := sql_stmt || sql_par3;

/* RUN THE QUERY */
OPEN QUEST_CUR FOR sql_stmt;

....


You can see that now the select statement has a variable number of return columns.
I would like to fetch the result of the query in a loop assigning the records to variables or to a record variable.
But after reading the documentation it looks that I can only declare record variables before the query string is built and there is not an easy way to fetch a cursor into a variable number of variables.

Is it possible to do what I am trying to do? Can you suggest a better approach?

Please help me,

TN
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 18 2009
Added on Oct 20 2009
7 comments
2,333 views