PostgreSQL ERROR: EXECUTE of SELECT ... INTO 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 that a particular command is not allowed (ie "SELECT ... INTO")? Or does it just mean that I am doing something wrong? The actual code causing the error is given below. I apologize if the answer is already here, however I looked and could not find this specific error. Thanks in advance ... For everything that costs, I run 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; 
+6
source share
1 answer

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

Instead, you can use CREATE TABLE AS SELECT.

  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)
+7
source

Source: https://habr.com/ru/post/953358/


All Articles