-- 目标输出视图对象 drop view if exists counting; create view counting as select coalesce(null,'无效机构编号') c_orgbh ,coalesce(null,0) total select * from counting; -- 函数模糊查询 drop function if exists userTotalFunc; CREATE OR REPLACE FUNCTION userTotalFunc() RETURNS setof counting AS $$ declare g record; declare r record; BEGIN -- 循环组织机构编号 FOR g IN (select c_orgbh from xh_ht.fs_yw_base_org) LOOP -- 循环模糊分组组织机构查询 FOR r IN (select c_orgbh, coalesce(count(*),0) total from xh_yw.xh_user_online_tb where c_orgbh like concat('''',g.c_orgbh,'%','''') group by c_orgbh ) LOOP return next r; END LOOP; END LOOP; END; $$ LANGUAGE plpgsql;
查询上述函数不会报错,但也没有结果输出,counting很有可能需要实际的物理表返回类型而不是视图,并且declare的类型是否正确有待考证。