SQL 状态:42601 当尝试在函数 PostgreSQL 中创建视图时
SQL state: 42601 when try to create view inside a function PostgreSQL
我在执行以下函数时得到 sql 状态 42601。有人可以帮助解决此错误。我使用 this Whosebug 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;
您在为 EXECUTE 语句连接字符串时至少搞砸了 3 次。用于创建视图的 SQL 似乎不是有效的,因为再次连接不正确。
我给你的建议:
第 1 次构建有效的 sql 用于视图创建
第 2 次小心地用变量连接替换所需的部分
第三,您可以随时检查日志文件以了解有关您遇到的错误的更多信息
祝你好运!
如果有人遇到同样的情况,我通过在参数名称周围添加三个单引号来解决这个问题,我想将其视为单引号字符串
EXECUTE
'create materialized view '||authority||' as
WITH FINDUNDERSCORE as
(select position(''_'' in '''||authority||''') as pos )
...
我在执行以下函数时得到 sql 状态 42601。有人可以帮助解决此错误。我使用 this Whosebug 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;
您在为 EXECUTE 语句连接字符串时至少搞砸了 3 次。用于创建视图的 SQL 似乎不是有效的,因为再次连接不正确。
我给你的建议:
第 1 次构建有效的 sql 用于视图创建
第 2 次小心地用变量连接替换所需的部分
第三,您可以随时检查日志文件以了解有关您遇到的错误的更多信息
祝你好运!
如果有人遇到同样的情况,我通过在参数名称周围添加三个单引号来解决这个问题,我想将其视为单引号字符串
EXECUTE
'create materialized view '||authority||' as
WITH FINDUNDERSCORE as
(select position(''_'' in '''||authority||''') as pos )
...