CREATE TABLE problem in plpgsql trigger

From: James Croft <james(dot)croft(at)lumison(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: CREATE TABLE problem in plpgsql trigger
Date: 2005-05-19 08:58:37
Message-ID: d6hkf8$207t$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all,

I'm trying to create a trigger function for a few tables that will store
old versions of rows prior to any update on them. Part of the function
needs to creates other tables (the table to store these snapshots in).

When this trigger runs I get the and error of 'syntax error at or near
"$1" at character 15' which is the CREATE TABLE line.

<snip>
DECLARE
rec RECORD;
snapshottable TEXT;
originaltable TEXT;
BEGIN
SELECT INTO rec count(*) AS num FROM pg_tables WHERE schemaname =
''table_snapshots'' AND tablename = TG_RELNAME;
IF rec.num < 1 THEN
snapshottable := ''table_snapshots.'' || TG_RELNAME;
originaltable := TG_RELNAME;
CREATE TABLE snapshottable (LIKE originaltable);
ALTER TABLE snapshottable ADD COLUMN snapshottime date;
ALTER TABLE snapshottable ALTER COLUMN snapshottime SET DEFAULT
CURRENT_TIMESTAMP;
END IF;
</snip>

The problems seems to be with the table_name arg being a variable and
not a literal but can't see how to fix this.

If anyone knows what's going on here or has any pointers it would be
appreciated.

Thanks,
James

Responses

Browse pgsql-general by date

  From Date Subject
Next Message James Croft 2005-05-19 09:07:23 Re: CREATE TABLE problem in plpgsql trigger
Previous Message Richard Huxton 2005-05-19 08:35:01 Re: How can I write trigger on a columns insert/update?