Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL ERROR: EXECUTE of SELECT ... INTO is not implemented

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;
like image 994
Guest Posting Avatar asked Sep 06 '13 19:09

Guest Posting


1 Answers

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)
like image 88
Pavel Stehule Avatar answered Nov 07 '22 03:11

Pavel Stehule