Search Postgresql Archives

Dynamic plpgsql help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux