SQL状态:42601时尝试在函数PostgreSQL中创建视图

时间:2015-11-02 22:05:00

标签: sql postgresql view sql-function

执行以下功能时,我得到sql状态42601。有人可以帮助解决此错误。我使用this stackoverflow question从函数创建视图。我的功能代码如下。我正在尝试使用select * from Test('DEPTA_');执行此功能。我收到错误

SQL state: 42601
Context: PL/pgSQL function test(text) line 3 at EXECUTE statement

功能代码:

create or replace function Test(authority text) returns void
as  $$
BEGIN
EXECUTE 
    'create materialized view '||authority ||' as
    WITH FINDUNDERSCORE as
    (select '||authority ||' as role, position(''_'' in '||authority||') as pos ),
    DISTINCT_ROLE as
    ( select  substring('||authority ||', 0, pos) as distinctRoles from FINDUNDERSCORE where position(''_'' in '||authority ||') > 1 and ''authority '' not like ''ROLE%''
        union select substring('||authority ||', pos+1, length('||authority ||')) as distinctRoles from FINDUNDERSCORE where position(''_'' in '||authority ||') > 1  and '||authority ||' not like ''ROLE%''
        union select '||authority ||' from FINDUNDERSCORE 
     ),

     ORIGINAL_ID as
     (select ROW_NUMBER() over(order by distinctRoles asc) as id, distinctRoles from DISTINCT_ROLE  order by distinctRoles asc),

    mapped_Id as
    ( select (case when oi.distinctroles ~ asid.sid then asid.id  end ) as newId, oi.id,oi.distinctroles,asid.sid, asid.id from original_id oi,acl_sid asid  ),

    AGGREGATE_NEWID as
    (select mi.newid,max(sid) sid, max(distinctroles) distinctroles, array_to_string(array_agg(mi.distinctroles),',') as aggregatedroles  from mapped_id mi where mi.newid is not null group by mi.newid ),

      MATCH_ACL_ENTRY as
      (select * from acl_entry ae join AGGREGATE_NEWID  asid on ae.sid = asid.newid and granting is true and  bitand(cast(ae.mask as bit(32)), cast(1 as bit(32)) ) = cast(1 as bit(32)) ) ,

       MATCH_ACL_OBJECT_IDENTITY as
      (select * from ACL_OBJECT_IDENTITY acl join MATCH_ACL_ENTRY asid on acl.id = asid.acl_object_identity),
        MATCH_ACL_PLATE as
        (select p.id, p.plate_barcode, p.plate_size, p.plate_id, acl.aggregatedroles, substring(acl.aggregatedroles,0,position(',' in acl.aggregatedroles)) as parentrole, 
        substring(acl.aggregatedroles,position(',' in acl.aggregatedroles)+1, length(acl.aggregatedroles)) as childrole from plate p join MATCH_ACL_OBJECT_IDENTITY acl on acl.object_id_identity = p.id)
        select id,plate_barcode,plate_size,plate_id from MATCH_ACL_PLATE';

END;
$$ LANGUAGE plpgsql;

2 个答案:

答案 0 :(得分:1)

在连接EXECUTE语句的字符串时,你至少搞砸了三次。由于再次使用不正确的连接,用于创建视图的SQL似乎不是有效的。

我向你推荐:

首先为视图创建构建一个有效的sql

第二次小心地用变量连接替换所需的部件

第3,您可以随时查看日志文件,以了解有关错误的更多信息

祝你好运!

答案 1 :(得分:0)

如果有人遇到同样的情况我通过在参数名称周围添加三个单引号来解决这个问题,我想将其视为单引号字符串

EXECUTE 
'create materialized view '||authority||' as
 WITH FINDUNDERSCORE as
 (select position(''_'' in '''||authority||''') as pos )
    ...