Can an oracle SQL query execute a string query selected from a table?

前端 未结 3 1552
迷失自我
迷失自我 2021-01-23 10:34

When using oracle SQL is it possible to run a query based on a text_string from a subquery? An example might clarify what I\'m trying to do

    select count(sql_         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-23 10:38

    Generally, this isn't a particularly good design-- storing SQL in tables and dynamically executing it introduces all sorts of security and maintenance issues.

    It is probably possible (though it's way too late on a Friday that started way too early for me to try to figure it out) to do a really cool XML query along the lines of this query that runs a count(*) against every table in the schema that would do this all in one query.

    For the vast majority of programmers, though, the simpler approach would be to loop over the queries, run them one at a time, and store the results somewhere. Potentially the local variable would be added to a collection of counts, for example.

    FOR q IN (SELECT sql_text FROM query_table)
    LOOP
      EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM (' || q.sql_text || ')'
         INTO some_local_variable;
      <>
    END LOOP;
    

    Since you're trying to create a view, you could take this logic and put it in a pipelined table function. You'd do a PIPE ROW to return data within the loop. Your view could then be created on top of the pipelined table function.

提交回复
热议问题