1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PosgreSQLメモ

Posted at

個人的なメモ帳を整理していたら出てきたので、共有したいと思います。
※ この内容は新しくもなく、かつ試した程度のただのメモ書きです。

一時的なループ

大量データ投入などに使える

CREATE TABLE public.hoge
(
    test1 bigint NOT NULL,
    test2 bigint NOT NULL
);

do $$
  begin
    for i in 1..10 loop
      for j in 1..10 loop
        insert into public.hoge(test1, test2) values (i, j);
      end loop;
    end loop;
  end;
$$;

DROP TABLE public.hoge;

PREPAREの利用

###準備
PREPARE文で作成する

PREPARE hoge_sql(integer, int)
AS
insert into public.hoge(test1, test2) values ($1, $2);

実行

EXECUTE文で実行

EXECUTE hoge_sql(1);

削除

一度作るとfunctionとして残るので、なるべく終わったらDEALLOCATEで削除する。

DEALLOCATE PREPARE hoge_sql;
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?