PL/pgSQL problem with variables - Mailing list pgsql-sql

From Hans-Jürgen Schönig
Subject PL/pgSQL problem with variables
Date
Msg-id 3B081C4F.AC9E539C@cybertec.at
Whole thread Raw
List 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



pgsql-sql by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: query PostgreSQL from c++
Next
From: Peter Eisentraut
Date:
Subject: Re: c++ wrapper library