Oracle SQL Query to to run a column value as sql query
I have the following row in the table
TEMP_ID HTML_LABEL HTML_NAME OPTIONS_TYPE OPTION_VALUES HTML_CODE
----------------------------------------------------------------------------------------------
2 RULE_NO : RULE_NO_7_32 D SELECT DRV_COLUMN FROM FRD_DATA <reconTags:re开发者_如何转开发nderSelect></reconTags:renderSelect>
I want an oracle sql query that gives the output like this
TEMP_ID HTML_LABEL HTML_NAME OPTIONS_TYPE OPTION_VALUES HTML_CODE
----------------------------------------------------------------------------
2 RULE_NO : RULE_NO_7_32 D 1,2,3,4 <reconTags:renderSelect></reconTags:renderSelect>
I want the result of the query stored in the option_values field to be displayed as the value of the option_values field. (Possibly comma-separated concatenated values, where the query would return multiple rows.)
Create a function that takes your sql column as a parameter, and loops through each record building up a string of values then return the result, the usage would be SELECT col1, your_function(col2) from your table
Here's some pointers on dynamic SQL: http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/dynamic.htm
I guess it will be something like this:
CREATE OR REPLACE FUNCTION sql_to_csv (p_select IN VARCHAR2)
RETURN VARCHAR
AS
v_out VARCHAR2 (4000);
TYPE RefCurTyp IS REF CURSOR;
v_cursor RefCurTyp;
a_record DUAL%ROWTYPE;
BEGIN
OPEN v_cursor FOR p_select;
-- Fetch rows from result set one at a time:
LOOP
FETCH v_cursor INTO a_record;
EXIT WHEN v_cursor%NOTFOUND;
v_out:=v_out || ',' || a_record.dummy;
END LOOP;
-- Close cursor:
CLOSE v_cursor;
RETURN (v_out);
END;
精彩评论