创建函数来创建表Postgresql

时间:2016-11-05 07:32:22

标签: postgresql

我正在尝试在POSTGRESQL函数中创建表,但是会出现以下错误。

  

错误:在CONTEXT中没有选择要创建的模式:SQL   语句“CREATE TABLE IF NOT NOT EXISTS test.t_testing(       id serial PRIMARY KEY,       customerid int,       日期记录日期,       值双精度)“

我的功能如下。

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 个答案:

答案 0 :(得分:2)

请改为尝试:

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'schema'和'table'。