Dynamic plpgsql help

From: Scott Geller <sgellergsu(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Dynamic plpgsql help
Date: 2010-04-04 16:18:14
Message-ID: h2lc598163c1004040918gce411fc9ybdf5fba189998274@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi

I have the following plpgsql function that works:

DROP TYPE if exists distr CASCADE;
CREATE TYPE distr AS (b bigint, var varchar);

DROP FUNCTION IF EXISTS dist() cascade;

CREATE or replace FUNCTION dist() RETURNS setof distr
AS $$ begin
return query EXECUTE 'Select count(*) as b, cast(mostype as varchar)
as var from segmentation group by mostype';
end $$ LANGUAGE 'plpgsql' VOLATILE;
;

select * from dist();

When I try to make it dynamic, I get the error:

ERROR: column "mostype" does not exist

DROP TYPE if exists distr CASCADE;
CREATE TYPE distr AS (b bigint, var varchar);

DROP FUNCTION IF EXISTS dist(text) cascade;

CREATE or replace FUNCTION dist(text) RETURNS setof distr
AS $$ begin
return query execute 'Select count(*) as b, cast( ' ||
quote_ident($1) || ' as varchar) as var from segmentation group by '
|| quote_ident($1);
end $$ LANGUAGE 'plpgsql' VOLATILE;
;

select dist(mostype);

Your help is appreciated

Scott

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ankuj Gupta 2010-04-04 16:18:32 Re: How to configure the postmaster daemon so that it accepts TCP connections?
Previous Message Scott Marlowe 2010-04-04 16:14:58 Re: How to configure the postmaster daemon so that it accepts TCP connections?