3

I am trying to create table inside POSTGRESQL function but getting following error.

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 )"

My function is as follows.

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 Answer 1

5

Try this instead:

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;

So sepparate 'schema' and 'table'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.