开发者

Oracle SQL Query to to run a column value as sql query

开发者 https://www.devze.com 2023-04-07 04:58 出处:网络
I have the following row in the table TEMP_IDHTML_LABEL HTML_NAMEOPTIONS_TYPE OPTION_VALUESHTML_CODE

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号