【问题标题】:How to execute a query multiple times in PostgreSQL如何在 PostgreSQL 中多次执行查询
【发布时间】:2016-07-21 14:28:17
【问题描述】:

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

我有一个查询要在表中插入一条记录

--类似这样的

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

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

【问题讨论】:

    标签: sql postgresql plpgsql


    【解决方案1】:

    这在不恢复到 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);
    

    【讨论】:

    • +1 它也比在本地表中插入一百万行随机数据的公认答案快约 30%
    【解决方案2】:

    尝试使用循环:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 2019-10-07
      • 2021-10-21
      • 1970-01-01
      相关资源
      最近更新 更多