How to run multiple sql statements in Postgres using the tool PGADMIN?
Asked Answered
U

1

8

I have 170 Alter Drop Constraint statement, 120 Truncate table, 120 Insert table and 170 Alter Add Constraint. All of these should be run in one in one script as batch script and I'm using PGADMIN IV tool.

Tried executing them between Begin and End as follows like Oracle,

BEGIN 
  ALTER Statement1..,,  ;
  ALTER Statement2..,,  ;
  TRUNCATE Statement3..,,  ;
  TRUNCATE Statement4..,,  ;
  INSERT Statement5..,,    ;
  INSERT Statement6..,,    ;
  COMMIT;
  ALTER Statement7..,,     ;
  ALTER Statement8..,,     ;
  ---
  -----
  -------
  ---------
  COMMIT;
END;

But its not working,

Can some one please give a suggestion on how I can do this?

Universalize answered 29/1, 2019 at 14:51 Comment(6)
You need a ; after each statement, not a , and you need a COMMIT; or ROLLBACK; at the end, end; is only valid in PL/pgSQL, not in SQL (which is the same in OracleTicon
Yeah, I did that but unfortunately forgot to post it here, I'm editing it right now! As far as my understanding is concerned we can't run SQL statements between BEGIN and END clause in Postgres, but rather write all the SQL's by itself and select everything at one shot and run it accordingly. Here I'm talking about running in PGADMIN IV tool and not PSQL. But now I'm just looking for a work around if there is any other alternative to run all the SQL's as one batch in PG ADMIN TOOL using PL/PGSQL if possible. Please correct me, if I'm wrong here,?Universalize
If you are looking for the equivalent of Oracle's anonymous PL/SQL block, then you are probably looking for the do commandTicon
Ok Nice, but lets say if I need to run three select statements in one batch in Postgres using PGADMIN tool, can you please let me know how can I accomplish that?Universalize
Sorry, I don't use pgAdminTicon
Aww, Ok But really appreciate your time for looking into this from your busy scheduleUniversalize
L
10

It is not very clear from your question what you actually intend to do. Is it that you want to run all your statements in one script or one transaction?

If you need to run them in one script, then just running them as follows in PGADMIN should work.

  ALTER Statement1 ;
  ALTER Statement2 ;
  TRUNCATE Statement3 ;
  TRUNCATE Statement4 ;
  INSERT Statement5 ;
  INSERT Statement6 ;
  ALTER Statement7 ;
  ALTER Statement8 ;

And so on.

On the other hand if you want to run them all in a transaction, then just enclose the statements in a BEGIN and COMMIT as follows.

  BEGIN ;
  ALTER Statement1 ;
  ALTER Statement2 ;
  TRUNCATE Statement3 ;
  TRUNCATE Statement4 ;
  INSERT Statement5 ;
  INSERT Statement6 ;
  ALTER Statement7 ;
  ALTER Statement8 ;
  COMMIT ;

I don't see any difference between how you would run this in PGADMIN versus how you would run this using a client like psql.

Libava answered 29/7, 2020 at 11:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.