2016-11-05 118 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