create or replace function test_bl(v_res_id int) returns setof tbl_res as $BODY$ declare v_res_level INT; rec tbl_res%rowtype; begin select res_level into v_res_level from test where res_id=v_res_id;
for rec in execute 'select * from test where res_tree_level'||v_res_level||'_id='||v_res_id||';' loop return next rec; end loop; return ; END; $BODY$ LANGUAGE 'plpgsql';