Postgresql

帶有 WHERE .. IN 數組參數的儲存過程

  • March 29, 2022

我想在表中與傳遞給儲存過程的行 ID 數組匹配的每個條目上設置一個布爾值。幫我破解正確的語法來完成這個?以下是我得到的最接近的。

CREATE PROCEDURE tester (id_list bigint[])
AS
$$
UPDATE some_table
SET touched = true
WHERE id IN (unnest(id_list));
$$ LANGUAGE sql;

CALL tester(ARRAY[12, 34]);

使用ANY運算符:

UPDATE some_table
 SET touched = true
WHERE id = ANY (id_list);

a_horse_with_no_name 的 ANY 答案更簡潔,但我還發現在 unnest 函式前面添加 SELECT 也可以:

CREATE PROCEDURE tester (id_list bigint[])
AS
$$
UPDATE some_table
SET touched = true
WHERE id IN (SELECT unnest(id_list));
$$ LANGUAGE sql;

引用自:https://dba.stackexchange.com/questions/310272