当我从我定义的函数运行以下命令时,出现错误“SELECT ... INTO的EXECUTE未实现”。这是否意味着不允许特定命令(即“SELECT ... INTO”)?或者它只是意味着我做错了什么?导致错误的实际代码如下。如果答案已经在这里,我道歉,但是我看起来并且找不到这个特定的错误。在此先感谢...无论如何,我都在运行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;
答案 0 :(得分:7)
INTO在这个用例中含糊不清,然后在那里被禁止。
您可以使用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)