[英]postgres procedure list of values
我正在嘗試將值列表傳遞到 Postgres 11 過程中。 查詢具有IN
條件。 如何將值列表傳遞給過程,然后如何在查詢中使用它?
create or replace procedure some_procedure(
source_id_list character varying <-- is this correct?
)
language plpgsql
as $$
declare
result_count int;
begin
select count(*)
into result_count
from table sd
where sd.id in source_id_list <-- syntax error
;
RAISE NOTICE 'result: %', result_count;
end;
$$
IN
需要一個值列表,而你給它一個字符串。
如果您可以將參數的數據類型更改為過程,那么使用數組會更簡單:
create or replace procedure some_procedure(source_id_list int[])
language plpgsql
as $$
declare
result_count int;
begin
select count(*)
into result_count
from table sd
where id = any(source_id_list);
raise notice 'result: %', result_count;
end;
$$
如果您無法更改數據類型的參數,則另一個選項使用string_to_array()
將字符串解析為數組:
select count(*)
into result_count
from table sd
where sd.id = any(string_to_array(source_id_list, ',')::int[]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.