关系不存在 PLPGSQL

Relation does not exist PLPGSQL

我正在尝试使用 plpgsql 在函数内部创建一个视图,其中 returns "small" table 的 x 列定义为(x 整数,y 整数)。

create or replace function skyline_naive2(dataset text) returns setof integer as
$$
declare 
    fullx text;
begin
    fullx = dataset||'_skyline_naive2';
    execute format('create view %s as select x,y from %s',fullx,dataset);
    return query select x from fullx;
end
$$ language plpgsql;

select * from skyline_naive2('small');

它returns"relation fullx does not exist"

我理解是因为没有fullx关系,但是我想用变量名调用视图

任何帮助都会

您需要EXECUTE您的动态查询:

RETURN QUERY EXECUTE 'SELECT x FROM ' || fullx;

select 使用动态 SQL(正如您对 create 使用的那样):

create or replace function skyline_naive2(dataset text) returns setof integer as
$$
declare 
    fullx text;
begin
    fullx = dataset||'_skyline_naive2';
    execute format('create view %I as select x,y from %I',fullx,dataset);
    return query execute format('select x from %I', fullx);
end
$$ language plpgsql;