PL/pgSQL problem with variables

From: Hans-Jürgen Schönig <hs(at)cybertec(dot)at>
To: pgsql-sql(at)postgresql(dot)org
Subject: PL/pgSQL problem with variables
Date: 2001-05-20 19:34:39
Message-ID: 3B081C4F.AC9E539C@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I want to write a function that creates a new column for every value
found in the table but something seems to go wrong with the variable:

CREATE FUNCTION transp() RETURNS int4 AS '
DECLARE
col RECORD;
cname varchar;
BEGIN
CREATE TABLE tmp_trans (label) AS
SELECT label FROM view_inc_age_1;
FOR col IN SELECT DISTINCT age_code FROM view_inc_age_1
LOOP
cname := col.age_code;
ALTER TABLE tmp_trans ADD COLUMN cname int4;
END LOOP;
RETURN 0;
END;
' LANGUAGE 'plpgsql';

persons=# SELECT transp();
ERROR: parser: parse error at or near "$1"

The problem occurs in the line containing "ALTER TABLE". Does anybody
know what is going wrong?

Hans

Browse pgsql-sql by date

  From Date Subject
Next Message Hans-Jürgen Schönig 2001-05-20 19:42:37 Transposing data
Previous Message Alexander Dederer 2001-05-20 19:34:11 Re: timestamp bug