特定のテーブル数のテーブルを作成する関数を作成しています。特定のテーブルを作成する前に、そのテーブルが既に存在するかどうかを確認したい:
create or replace function test (numoftables int) returns void as $$
declare
i integer;
begin
i:=0;
while i< numoftables loop
if not exists (select * from table$i$) -- check if table exists
then
create table table$i$(
column1 int,
column2 int,
column1 text,
column2 text,
column3 text);
end if;
end loop;
end;
$$
language 'plpgsql';
このコードを実行すると、エラーが発生しました。
ERROR: relation "table$i$" does not exist LINE 1: SELECT not exists (select * from table$i$)
このコードを変更して正しく動作させる方法を教えてください。