列在 PostgreSQL 视图中不存在

Column doesn't exist in PostgreSQL View

我遇到以下情况,但我不明白我的代码有什么问题。我收到此错误:列 vx1 不存在(vx1 是变量而不是列)。

    vx1 double precision;
    vy1 double precision;
    vz1 double precision;
    vx2 double precision;
    vy2 double precision;
    vz2 double precision;
begin
    vx1 := x1;
    vy1 := y1;
    vz1 := z1;
    vx2 := x2;
    vy2 := y2;
    vz2 := z2;

    create view "shortestpathEdges" as
    select *
    from tbledges te
    where te.x<=vx1 and te.y<=vy1 and te.z<=vz1 and 
          te.x<=vx2 and te.y<=vy2 and te.z<=vz2;

这是完整的功能,但它让你感觉不太好,所以我把它做得很简单,因为仍然存在错误。

create temp view shortestpathEdges as
select *
from(select x , y, z, vid   
     from(select tv.x as x,tv.y as y,tv.z as z,tv."VertexID" as vid
      from (select te."EdgeID" edgeid, te."VertexID" vertexid
        from tbledges te
        where te.status='dual')as t1, tblvertices as tv
      where t1.vertexid=tv."VertexID") as tv2
     where tv2.x<=vx1 and tv2.y<=vy1 and tv2.z<=vz1 and 
         tv2.x<=vx2 and tv2.y<=vy2 and tv2.z<=vz2) as tv3, tbledges as tble
where tv3.vid=tble."VertexID";

PL/Pgsql 帮助文档的 Variable Substitution 部分提到了答案(可以说是隐晦的)。

Variable substitution currently works only in SELECT, INSERT, UPDATE, and DELETE commands, because the main SQL engine allows query parameters only in these commands. To use a non-constant name or value in other statement types (generically called utility statements), you must construct the utility statement as a string and EXECUTE it.

这意味着 PL/Pgsql 中的变量替换不会发生在 DDL 命令上(目前)。

为此,您应该使用 EXECUTE 语句(link 有关于如何构建变量 sql_string 的示例,可以通过 EXECUTE sql_string;)