Question:

How do you display SQL code without running it?

by  |  earlier

0 LIKES UnLike

I would like to run a query - and display both the code & the results. I am using SQL & Oracle.

 Tags:

   Report

2 ANSWERS


  1. One way could be to include the query into a string in the dual table

    SELECT 'SELECT * FROM table' AS "Code"

    FROM dual

    /

    SELECT *

    FROM table

    /


  2. If all you want is some sort of documentation to precede your result set, the easiest way to do it would be a display via dual as the previous responder suggests. If you want to incorporate it into the SQL itself, you could do something like:

    SELECT 'Code:', ...list of empty fields corresponding to actual result set, 'SELECT ...'

    UNION

    SELECT '', ...desired result set fields.... ''  //note that these are two sets of two quotes, indicating two empty strings

    ORDER BY 1 DESC, ... // assumes that a non-empty string appears after an empty one in collating sequence

    This would produce

    Code: .... a bunch of empty fields .... SELECT field1, ... FROM...

    <space> ...actual data values.......... <space>

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.