我正在使用执行格式执行SQL查询,这需要将多个变量作为参数传递。在这里,我的查询需要很多符号,例如:'%' and ','
因此我的查询非常大,超过了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','%','%',',','%,%',',','%','%',',','%,%',',','%','%',',','%,%',',','%','%',',','%,%',',','%','%',',','%,%',',')
答案 0 :(得分:1)
根据docs
的数组
concat
,concat_ws
和format
函数是可变参数,因此它是 可以传递要连接或格式化为的值 标有VARIADIC
关键字
您可以使用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);
错误:不能将超过100个参数传递给函数
as array:
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排)
但我会停下来思考 - 你真的想传递100个参数吗?