Question

When I run the following command from a function I defined, I get the error "EXECUTE of SELECT ... INTO is not implemented". Does this mean the specific command is not allowed (i.e. "SELECT ...INTO")? Or does it just mean I'm doing something wrong? The actual code causing the error is below. I apologize if the answer is already out here, however I looked and could not find this specific error. Thanks in advance... For whatever it's worth I'm running 8.4.7

 vCommand = 'select ' || stmt.column_name || ' as id ' ||
            ', count(*) as nCount
            INTO tmpResults
            from ' || stmt.table_name || '
            WHERE ' || stmt.column_name || ' IN (select distinct primary_id from anyTable
                                                WHERE primary_id = ' || stmt.column_name || ')
            group by ' || stmt.column_name || ';';
EXECUTE vCommand;
Was it helpful?

Solution

INTO is ambiguous in this use case and then is prohibited there.

You can use a CREATE TABLE AS SELECT instead.

CREATE OR REPLACE FUNCTION public.f1(tablename character varying)
 RETURNS integer
 LANGUAGE plpgsql
AS $function$
begin
  execute 'create temp table xx on commit drop as select * from ' 
                                      || quote_ident(tablename);
  return (select count(*) from xx);
end;
$function$

postgres=# select f1('omega');
 f1 
────
  2
(1 row)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top