存储过程-为IN子句传递多个值

时间:2019-10-04 21:22:33

标签: amazon-redshift plpgsql

我正在为Postgres编写存储过程,只需执行select * from table where id in (value1, value2, ...)

这些值将从变量中获取。

我的代码:

CREATE OR REPLACE PROCEDURE record_example(v_name varchar(100), v_id int)
LANGUAGE plpgsql
AS $$
DECLARE
  rec RECORD;
  BEGIN
  FOR rec IN select id, updated from mytable where names in (v_name) and id=v_id
  LOOP
     RAISE INFO 'id = % and updated = %', rec.id, rec.updated;
END LOOP;
END;
$$; 

如果我为v_name使用单个值,则此方法实际上有效。

例如:

call record_example('myname',101);

但是,如果我执行多个值,则无法正常工作。

帮助1:

call record_example('myname, your_name',101);

它刚刚返回CALL,就是这样。没啥事儿。

帮助2:

有时v_id变量是可选的,因此,FOR循环应包含id=v_id

例如:

FOR rec IN select id, updated from mytable where names in (v_name) 
  LOOP
     RAISE INFO 'id = % and updated = %', rec.id, rec.updated;
END LOOP;

0 个答案:

没有答案