2016-11-05 119 views
1

我想在POSTGRESQL函數內創建表,但得到以下錯誤。創建函數來創建表Postgresql

ERROR: no schema has been selected to create in CONTEXT: SQL statement " CREATE TABLE IF NOT EXISTS test.t_testing ( id serial PRIMARY KEY, customerid int, daterecorded date, value double precision )"

我的功能如下。

CREATE OR REPLACE FUNCTION test.create_table_type1(t_name varchar(30)) 
    RETURNS VOID AS 
$func$ 
BEGIN 

EXECUTE format(' 
    CREATE TABLE IF NOT EXISTS %I (
    id serial PRIMARY KEY, 
    customerid int, 
    daterecorded date, 
    value double precision 
    )', 'test.t_' || t_name); 

END 
$func$ LANGUAGE plpgsql; 

回答

1

試試這個:

CREATE OR REPLACE FUNCTION test.create_table_type1(t_name varchar(30)) 
    RETURNS VOID AS 
$func$ 
BEGIN 

EXECUTE format(
    'CREATE TABLE IF NOT EXISTS %I.%I (
     id serial  PRIMARY KEY, 
     customerid  int, 
     daterecorded date, 
     value   double precision 
    )', 
    'test', 
    ('t_' || t_name) 
); 

END 
$func$ LANGUAGE plpgsql; 

所以sepparate '模式' 和 '表'。

+0

謝謝克里斯託。它解決了我的問題。 – karan