Vishal D
Vishal D

Reputation: 51

Using execute in subquery in postgresql

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?

Upvotes: 0

Views: 866

Answers (1)

user330315
user330315

Reputation:

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;

Upvotes: 2

Related Questions