Postgresql 迭代函数返回的表
我声明了两个函数(duplicate_iofs和duplicate_ioftypes),它们都在执行插入操作后返回一个表:Postgresql 迭代函数返回的表,postgresql,plpgsql,Postgresql,Plpgsql,我声明了两个函数(duplicate_iofs和duplicate_ioftypes),它们都在执行插入操作后返回一个表: CREATE OR REPLACE FUNCTION duplicate_iofs(IN iof_group_src_id integer, IN iof_group_dst_id integer) RETURNS TABLE(src_id integer, new_id integer) AS $$ BEGIN -- DO INSERT REQUESTS
CREATE OR REPLACE FUNCTION duplicate_iofs(IN iof_group_src_id integer, IN iof_group_dst_id integer)
RETURNS TABLE(src_id integer, new_id integer) AS $$
BEGIN
-- DO INSERT REQUESTS
END;
CREATE OR REPLACE FUNCTION duplicate_ioftypes(IN iof_group_src_id integer, IN iof_group_dst_id integer)
RETURNS TABLE(src_id integer, new_id integer) AS $$
BEGIN
-- DO INSERT REQUESTS
END;
如何在第三个函数中执行类似操作:
-- 1. Call duplicate_iofs(...) and store the result table (e.g. iofs_table)
-- 2. Call duplicate_ioftypes(...) and store the result table (e.g. ioftypes_table).
-- 3. Iterate through 'iofs_table' and 'ioftypes_table' with nested loops :
FOR iof_row IN SELECT * FROM iofs_table LOOP
FOR ioftype_row IN SELECT * FROM ioftypes_table LOOP
-- DO SOMETHING
END LOOP;
END LOOP;
注意:duplicate_iofs()和duplicate_ioftypes()只能调用一次,因此不能被调用到嵌套循环中。您可以尝试以下方法:
DECLARE
curs_iof CURSOR FOR SELECT * FROM iofs_table;
curs_ioftype CURSOR FOR SELECT * FROM ioftypes_table;
BEGIN
OPEN SCROLL curs_iof;
OPEN SCROLL curs_ioftype;
FETCH curs_iof INTO iof_row;
WHILE FOUND LOOP
FETCH FIRST FROM curs_ioftype INTO ioftype_row;
WHILE FOUND LOOP
-- DO SOMETHING
FETCH curs_ioftype INTO ioftype_row;
END LOOP;
FETCH curs_iof INTO iof_row;
END LOOP;
CLOSE curs_iof;
CLOSE curs_ioftype;
END;
详细信息。您使用的是什么版本的postgresql?为什么不使用函数来代替函数?@IgorRomanchenko我更喜欢使用函数而不是插入。。。返回将执行的处理分解为函数:这些函数由其他函数调用。