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?

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

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.
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.