CREATE TABLE from inside a function... - Mailing list pgsql-general

From Dominic J. Eidson
Subject CREATE TABLE from inside a function...
Date
Msg-id Pine.LNX.4.10.10008151036360.28080-100000@morannon.the-infinite.org
Whole thread Raw
Responses Re: CREATE TABLE from inside a function...  (Stephan Szabo <sszabo@megazone23.bigpanda.com>)
List pgsql-general
Is there a specific reason why one can not do a CREATE TABLE from inside a
pl/pgsql function? I've been trying to get some triggers working, and they
all keep erroring with the same error...

The following schema:
CREATE TABLE "spares_dbs" (
        "dbs_id" serial,
        "dbs_name" character varying(20),
        "dbs_desc" text
);

And the following trigger:

CREATE FUNCTION spares_trig_insert_dbs() RETURNS opaque AS '
BEGIN
  IF NEW.dbs_name ISNULL THEN
    RAISE EXCEPTION ''% cannot be NULL'', NEW.dbs_name;
  END IF;

  CREATE TABLE NEW.dbs_name ( id int, oem text, price float );
  RETURN NEW;
END;
' LANGUAGE 'plpgsql';

CREATE TRIGGER spares_test BEFORE INSERT ON spares_dbs FOR EACH ROW
EXECUTE PROCEDURE spares_trig_insert_dbs();

spares=# INSERT INTO spares_dbs VALUES ( NULL, 'test', 'Test table' );
ERROR:  parser: parse error at or near "$1"

Looking at the postmaster log (-d 6), reveals the following:

StartTransactionCommand
query: INSERT INTO spares_dbs VALUES ( NULL, 'test', 'Test table' );
parser outputs:

... snip parser output ...

ProcessQuery
query: SELECT   $1  ISNULL
parser outputs:

... snip more parser output ...

query: CREATE TABLE  $1  ( id int, oem text, price float )
ERROR:  parser: parse error at or near "$1"
DEBUG:  Last error occured while executing PL/pgSQL function
spares_trig_insert_dbs
DEBUG:  line 5 at SQL statement
AbortCurrentTransaction

My understand was it should have replaced "NEW.dbs_name" with "test"
(according to the INSERT data)...


--
Dominic J. Eidson
                                        "Baruk Khazad! Khazad ai-menu!" - Gimli
-------------------------------------------------------------------------------
http://www.the-infinite.org/              http://www.the-infinite.org/~dominic/





pgsql-general by date:

Previous
From: Chris Bitmead
Date:
Subject: Re: Search (select) options
Next
From: Ian Turner
Date:
Subject: Re: Referential integrity