【问题标题】:SQL state: 42601 when try to create view inside a function PostgreSQLSQL 状态:尝试在函数 PostgreSQL 中创建视图时出现 42601
【发布时间】:2015-11-02 22:04:59
【问题描述】:

执行以下函数时,我得到 sql state 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;

【问题讨论】:

    标签: sql postgresql view sql-function


    【解决方案1】:

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

    我给你的建议:

    第一次为创建视图构建一个有效的 sql

    第二次小心地用可变连接替换需要的部分

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

    祝你好运!

    【讨论】:

    • 感谢您的回复。您能告诉我连接错误的至少三个实例是什么吗?当我将权限变量替换为字符串常量时,用于创建视图的 sql 起作用。
    • 没问题,请找到字符组合 ',' 单引号实际上是关闭字符串,它必须被转义。
    • 我发现我必须使用三个 ' 才能将其视为单引号字符串并更改重复的列名。
    【解决方案2】:

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

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

    【讨论】:

    • 2 个单引号变成一个 qoute(这是如何转义引号的方法),第三个是用于关闭字符串并使用变量连接,因此结果将是 (select position(' _ ' in 'DEPTA_') as pos ) - 那么它看起来像一个很好的查询(与它将生成的原始版本相比(select position('_' in DEPTA_) as pos )并且你得到一个关于 DEPTA_ 的错误)考虑权限设置到“DEPTA_”字符串
    猜你喜欢
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 2013-09-12
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多