0

I am creating a dynamic query in my function from which I need to extract data. now I want to execute that query.

something like this:

declare
df_id varchar;
BEGIN
/*creating dynamic query in run time and saving it in df_id in string format
say df_id='select col from schema.table_name**'*/
CREATE TEMP TABLE temp_table ON COMMIT DROP AS
    execute df_id;

How can I do this? Also, Is there any other way that I can execute the query in string variable and store it to temp table?

Vishal D
  • 51
  • 9

1 Answers1

2

You need to include the CREATE TABLE in your dynamic SQL.

Assuming that the variable df_id already contains the SELECT statement, you can do something like this:

df_id := 'CREATE TEMP TABLE temp_table ON COMMIT DROP AS '||df_id;
execute df_id;
  • Thanks that worked. Can I store this data in array also? if yes How? because in some cases I am not allowed to use temp table. – Vishal D May 04 '18 at 11:05
  • @VishalD: yes that's possible, but you should ask a new question for that. Do not extend the scope of your question once you have an answer –  May 04 '18 at 11:06