Re: Dynamic plpgsql help

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Scott Geller <sgellergsu(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Dynamic plpgsql help
Date: 2010-04-04 16:34:20
Message-ID: y2s162867791004040934qa8a57cc1n6cefdfefb5e62ce3@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2010/4/4 Scott Geller <sgellergsu(at)gmail(dot)com>:
> 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);

maybe select dist('mostype')

regards

Pavel Stehule
>
> Your help is appreciated
>
> Scott
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2010-04-04 16:36:23 Re: How to configure the postmaster daemon so that it accepts TCP connections?
Previous Message Ankuj Gupta 2010-04-04 16:33:05 Re: How to configure the postmaster daemon so that it accepts TCP connections?