The goal of this program is to match a form's element IDs corresponding to different questions with the actual questions, so the resulting table's column names are the questions themselves instead of the ID numbers.
I am generating a query with pl/sql, and I know the generating part works since I can copy and paste the result into SQL Workshop where it returns the desired table. However, when I try to put it in "PL/SQL Function Body Returning a SQL Query", I get this error after running it.
ORA-00904: "D"."DUMMY": invalid identifier
Here is the code that generates the query.
DECLARE
p_formid VARCHAR2(20);
v_sql CLOB;
v_element_sql CLOB := '';
v_other_sql CLOB := '';
BEGIN
p_formid := :P3_NATHAN;
-- Build dynamic ELEMENT_ columns
BEGIN
SELECT LISTAGG(
'ELEMENT_' || e.ID || ' AS "' || e.TITLE || '"',
', '
) WITHIN GROUP (ORDER BY e.ID)
INTO v_element_sql
FROM U_FB_FORM_ELEMENT e
JOIN USER_TAB_COLUMNS c
ON c.TABLE_NAME = 'U_P_' || p_formid || '_FORMS'
AND c.COLUMN_NAME = 'ELEMENT_' || TO_CHAR(e.ID)
WHERE e.U_FB_FORM_ID = p_formid
AND e.TITLE IS NOT NULL
AND LENGTH(e.TITLE) > 0;
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_element_sql := '';
END;
-- Get other columns
BEGIN
SELECT LISTAGG(COLUMN_NAME, ', ')
INTO v_other_sql
FROM USER_TAB_COLUMNS
WHERE TABLE_NAME = 'U_P_' || p_formid || '_FORMS'
AND COLUMN_NAME NOT LIKE 'ELEMENT_%';
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_other_sql := '';
END;
IF v_other_sql IS NULL THEN
RETURN 'SELECT DUMMY FROM DUAL';
END IF;
v_sql := 'SELECT ' || v_other_sql;
IF v_element_sql IS NOT NULL AND LENGTH(TRIM(v_element_sql)) > 0 THEN
v_sql := v_sql || ', ' || v_element_sql;
END IF;
v_sql := v_sql || ' FROM U_P_' || p_formid || '_FORMS';
RETURN v_sql;
END;
When I print the query, "Dummy" does not show up anywhere. Looking at the debug:
select i.*, count(*) over () as APEX$TOTAL_ROW_COUNT
from (select "DUMMY"
from ((select /*+ qb_name(apex$inner) */d."DUMMY" from (SELECT ID, APPROVAL_STATUS, ...)
I am not sure why it is still selecting "DUMMY" given v_other_sql is not null.
The bind variable :P3_RESULT holds the generated query, but I cannot do
BEGIN
:P3_RESULT;
END;
Any idea/possible fixes for "Dummy"?
:P3_NATHANthat could get empty while using it in a "PL/SQL Function Body Returning a SQL Query"?:P3_NATHANis for the form id of the form I am pulling. After selecting a form from a drop-down menu, the page refreshes and returns the query in a text area region. For the text area, the type is just "PL/SQL Function Body". That works fine, so I do not think it gets empty, but I am trying to run the query on an interactive report region through "PL/SQL Function Body Returning a SQL Query", and that is where I am running into the issue.IFstatement forSELECT DUMMY FROM DUALtriggers (correctly), and it does provide the correct table on the page. However, as soon as I choose a form, the error happens.U_P_x_FORMStable. At least for the first column. Because that first "simple" query is what gets parsed by APEX to infer the first column name in both cases (when running the dummy query of theIF, but also when running the real, dynamic query). So would maybe aSELECT DUMMY AS ID FROM DUALwould make this hardcoded query compatible with the dynamically generated ones.