2016-12-14 91 views
0

我正在執行一個使用執行格式的SQL查詢,它需要將多個變量作爲參數傳遞。在這裏我的查詢需要很多符號,例如:'%' and ','Postgresql:執行格式查詢超過100個參數

因此,我的查詢已經非常大,超過了100個參數的限制。那麼有沒有辦法在查詢本身中包含'%' and ','而不是將它作爲參數傳遞。

查詢:

execute format('CREATE MATERIALIZED VIEW %I AS 
     SELECT id, 
       (select count(*) from friends bob where (i.name ilike any (string_to_array(replace(concat(%L,bob.keywords,%L),%L,%L),%L)) or 
i.description ilike any (string_to_array(replace(concat(%L,bob.keywords,%L),%L,%L),%L)) or 
i.additional_info ilike any (string_to_array(replace(concat(%L,bob.keywords,%L),%L,%L),%L)) or 
i.eventful_category ilike any (string_to_array(replace(concat(%L,bob.keywords,%L),%L,%L),%L)) or 
i.other_category ilike any (string_to_array(replace(concat(%L,bob.keywords,%L),%L,%L),%L)))) as friend_bob 
from events i ','FrienView','%','%',',','%,%',',','%','%',',','%,%',',','%','%',',','%,%',',','%','%',',','%,%',',','%','%',',','%,%',',') 
+1

如何和你在哪裏創建你的查詢字符串? –

+0

@TimBiegeleisen用查詢更新了我的問題。這只是查詢的一部分,進一步它將包括相似類型的查詢以根據需要生成更多列 –

+0

在MOUNT DOOM中創建的查詢。 –

回答

1

docs

The concat , concat_ws and format functions are variadic, so it is possible to pass the values to be concatenated or formatted as an array marked with the VARIADIC keyword

您可以使用ARRAY克服的侷限性,如: 準備:

with n as (select generate_series(1,101,1) g) 
    select $$SELECT format('$$||string_agg('%s',',')||$$',$$||string_agg(g::text,',')||')' 
    from n;  

運行:

SELECT format('%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s',1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101); 

ERROR: cannot pass more than 100 arguments to a function

爲數組:

SELECT format('%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s' 
,VARIADIC ARRAY[1, 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101]); 

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101 (1 row)

但我會停下來思考在這裏 - 你真的想通過100個參數..