我正在为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);
但是,如果我执行多个值,则无法正常工作。
call record_example('myname, your_name',101);
它刚刚返回CALL,就是这样。没啥事儿。
有时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;