14

What is the PostgreSQL equivalent to the TSQL “go” statement?

I have a query to insert a record into a table

--something like this

Insert into employee values(1,'Mike');
GO n;

I want this query to be executed n number of times.

3 Answers 3

25

This is possible without reverting to PL/pgSQL:

Insert into employee (id, name)
select 1,'Mike'
from generate_series(1,3);

Or if you want different IDs for each row:

Insert into employee (id, name)
select id,'Mike'
from generate_series(1,3) as t(id);
Sign up to request clarification or add additional context in comments.

1 Comment

+1 it is also faster by about 30% here than the accepted answer inserting a million rows in a local table with random data
21

try using loop:

do
$$
declare 
  i record;
begin
  for i in 1..3 loop
    Insert into employee values(1,'Mike');
  end loop;
end;
$$
;

Comments

1

https://xzilla.net//blog/2020/Nov/Do-This-10-Times-And-Stop-dot-dot-dot-In-Postgres.html

select $$Insert into emp  values(1,'Mike') $$ from generate_series(1,10) \gexec

only applicable to psql https://www.postgresql.org/docs/current/app-psql.html

Comments

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.