0

I have a file named insert_all.sql which contains the content as below. Here v1 is the parameter to be passed.

 do $$
    begin
    delete from tabledetails where table_name = '$(v1)';
    end;
    $$;

I am trying to execute the query in that file (insert_all.sql) with the command as given below. But it is not working. What's wrong with my command? If it is wrong, advice me on this.

psql.exe -U postgres -p 5454 -h 127.0.0.1 -d desk -f D:\insert_all.sql -v v1='statusTable'
2
  • link In this, I have to send the paramter with the file. Commented Dec 20, 2018 at 18:28
  • Possible duplicate of Pass command line args to sql (Postgres) Commented Dec 20, 2018 at 19:56

1 Answer 1

1

The variable option works for simple queries in the file but doesn't work inside DO blocks. One option is to create a TEMP table storing the variable and then use it like this.

create temp table var_temp as select :'v1'::TEXT as var;
 do $$
    begin
    delete from tabledetails where table_name =(select var from var_temp) ;
    end;
 $$; 

psql.exe -U postgres -p 5454 -h 127.0.0.1 -d desk -f D:\insert_all.sql -v v1="statusTable"

If you do not have any other operations that require a DO block, consider running the delete as plain SQL statement instead.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.