I have a long query that returns 1 column and 1 row:
Query 1:
select test_query from (
SELECT
LISTAGG('...') ... AS xx,
LISTAGG('"' || c.COLUMN_NAME || '"', ', ') WITHIN GROUP(ORDER BY c.COLUMN_NAME) AS column_list
.....
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE TABLE_NAME = xx);
The output looks like this. It has a query that I want to run. Note that it consists of quotation marks. The original query 1 also has quotation marks in the LISTAGG.
output:
test_query
select "col1", "col2" from stg.new
I wanted to extend the Query A such that I can select and run the test_query and return the final output of the test_query instead of queryA. I tried using execute immediate with $$:
execute immediate
$$select test_query from (
SELECT
LISTAGG('...') ... AS xx,
LISTAGG('"' || c.COLUMN_NAME || '"', ', ') WITHIN GROUP(ORDER BY c.COLUMN_NAME) AS column_list
.....
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE TABLE_NAME = xx)$$;
but instead of running the test_query it outputs the same thing as Query1.
What else can I try?
Here's an attempted conversion of your SQL template. You can extend it to add the entire SQL statement:
In case that's not working because of context, etc., here is a completely self-contained sample that's working:
Output: