0
我正在寫一個函數來爲給定的表計數創建表。創建一個特定的表之前,我想檢查表是否已經存在:PL/pgSQL函數不能正常工作
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$)
誰能告訴我怎樣才能改變這種代碼都正常工作。
語言名是一個普通的SQL標識,這意味着你不應該把它變成單引號。請改用'language plpgsql'。將來可能會刪除對單引號語言名稱的支持。順便說一句:爲什麼你不使用'CREATE TABLE IF NOT EXISTS ...'? –