格式错误的数组文字:数组值必须以“{”或维度信息开头

问题描述 投票:0回答:1

我正在使用postgres函数循环另一个使用dblink提交更新到我的数据库的函数。

这是“内部”功能的一部分:

    selectedIds := array(select id from items where id2 is null and id > latestItemId order by id asc  limit _limit);
highestItemId := (select max(x) from unnest(selectedIds) x);
updatedItemCount := array_length(selectedIds, 1);
raise notice 'updatedItemCount %', updatedItemCount;
raise notice 'SelectedItemiDs %', selectedIds;

PERFORM dblink_connect('dblink_trans','dbname=notified-local port=5432 user=postgres');
PERFORM dblink('dblink_trans','update items set id2 = id where id = any(' || selectedIds || ')');
PERFORM dblink('dblink_trans','COMMIT;');
PERFORM dblink_disconnect('dblink_trans'); 

我对selectedIds的加注通知如下:{23,60,65,66,588,968,1049,1198,1236,1356,1358,1359,1360,1364,1365,1366}

然后当我尝试进行dblink更新时,我在标题中得到错误。

错误说我应该{作为我的阵列的开头,从我在加注通知中可以看到的,这正是我所拥有的?

postgresql dblink
1个回答
0
投票

您的数组文字缺少单引号。最好使用format来避免SQL注入问题:

PERFORM dblink('dblink_trans',
               format('update items set id2 = id where id = any(%L)',
                      selectedIds)
              );
© www.soinside.com 2019 - 2024. All rights reserved.