2015-11-02 40 views
0

我在執行下面的函數時得到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; 

回答

0

如果有人穿過相同的情況下運行,我加入圍繞參數名三個單引號,我要考慮的單引號字符

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

2個單引號轉成一個qoute(這是如何轉義引號的方式),第3個是用變量關閉字符串和concat,所以結果將會是(選擇'DEPTA_'中的位置('_'))作爲pos ) - 那麼它看起來像一個很好的查詢(與它將生成的原始版本相比較(選擇位置(在DEPTA_中爲'_')爲pos),並且考慮到權限設置爲'DEPTA_'字符串,您會收到有關DEPTA_的錯誤 – alextunick

1

而對於串聯EXECUTE語句字符串你搞砸了至少三次。由於不正確的連接,用於創建視圖的SQL似乎不是有效的。

我推薦給大家:

1日建立視圖創建一個有效的SQL

第二小心地用可變串聯更換所需的零部件

第三,你可以隨時檢查日誌文件,以瞭解更多信息你得到的錯誤

祝你好運!

+0

感謝您rply解決了這個問題。你能告訴我至少有三個連接錯誤的實例嗎?當我將權限變量替換爲字符串常量時,用於創建視圖的sql工作正常。 – somename

+0

確定的事情,請找到字符的組合*','**單引號實際上關閉字符串,它必須逃脫。 – alextunick

+0

我想通了,我不得不使用三個'把它視爲單引號字符串,並更改重複的列名稱。 – somename