我在執行下面的函數時得到sql狀態42601。有人可以幫助解決這個錯誤。我用this stackoverflow question來創建函數的視圖。我的功能代碼如下。我正在嘗試使用select * from Test('DEPTA_');
執行此功能。我收到錯誤SQL狀態:42601當試圖在函數內部創建視圖時PostgreSQL
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個單引號轉成一個qoute(這是如何轉義引號的方式),第3個是用變量關閉字符串和concat,所以結果將會是(選擇'DEPTA_'中的位置('_'))作爲pos ) - 那麼它看起來像一個很好的查詢(與它將生成的原始版本相比較(選擇位置(在DEPTA_中爲'_')爲pos),並且考慮到權限設置爲'DEPTA_'字符串,您會收到有關DEPTA_的錯誤 – alextunick