Re: Dynamic plpgsql help

From: Sreelatha G <sreetlatha(at)gmail(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Scott Geller <sgellergsu(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Dynamic plpgsql help
Date: 2010-04-05 04:40:47
Message-ID: k2hf40ed141004042140p39ee17a1m6621a1b0e300d401@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

instead of cast(mostype as varchar) try mostype::varchar

Thanks
Sreelatha

On Sun, Apr 4, 2010 at 10:04 PM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>wrote:

> 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
> >
>
> --
> 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

Browse pgsql-general by date

  From Date Subject
Next Message Magnus Hagander 2010-04-05 09:57:04 Re: Completely wrong row estimates
Previous Message CaT 2010-04-05 03:28:06 Re: Null vs. Empty String in Postgres 8.3.8