如何在 PostgreSQL 中多次执行查询

How to execute a query multiple times in PostgreSQL

什么是 PostgreSQL 等同于 TSQL 的“go”语句?

我有一个查询要将记录插入 table

--像这样

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

我希望这个查询被执行 n 次。

尝试使用循环:

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

无需恢复到 PL/pgSQL 即可实现:

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

或者,如果您希望每行使用不同的 ID:

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