Oracle
 sql >> Database >  >> RDS >> Oracle

Eseguire query utilizzando un'istruzione all'interno di una colonna VARCHAR2

Questo tipo di SQL dinamico in SQL può essere creato con DBMS_XMLGEN.getXML . Anche se la query sembra un po' strana, potresti prendere in considerazione un design diverso.

Innanzitutto, ho creato una tabella e una riga di esempio utilizzando il tuo DDL. Non sono sicuro di cosa stai cercando di fare con le condizioni, quindi le ho semplificate in due righe con condizioni più semplici. La prima riga corrisponde alla prima condizione e nessuna riga corrisponde alla seconda condizione.

--Create sample table and row that matches the condition.
CREATE TABLE test_tab(
    date_column DATE,
    frequency NUMBER,
    test_statement VARCHAR2(255)
)
/

insert into test_tab values(sysdate, 1, 'frequency = 1');
insert into test_tab values(sysdate, 2, '1=2');
commit;

Ecco la query grande e restituisce solo la prima riga, che soddisfa solo la prima condizione.

--Find rows where ROWID is in a list of ROWIDs that match the condition.
select *
from test_tab
where rowid in
(
    --Convert XMLType to relational data.
    select the_rowid
    from
    (
        --Convert CLOB to XMLType.
        select xmltype(xml_results) xml_results
        from
        (
            --Create a single XML file with the ROWIDs that match the condition.
            select dbms_xmlgen.getxml('
                select rowid
                from test_tab where '||test_statement) xml_results
            from test_tab
        )
        where xml_results is not null
    )
    cross join
    xmltable
    (
        '/ROWSET/ROW'
        passing xml_results
        columns
            the_rowid varchar2(128) path 'ROWID'
    )
);