CREATE OR REPLACE FUNCTION "public"."get_sub_org"("p_class_id" text, "p_dept_id" text)
RETURNS TABLE("the_dept_id" varchar) AS $BODY$
BEGIN
-- Routine body goes here...
/*获取机构下所有的子机构ID*/
IF (p_dept_id is null or p_dept_id = '') THEN
RETURN QUERY
SELECT department_id FROM jc_rankdiagram WHERE class_id = p_class_id group by department_id;
ELSE
RETURN QUERY
WITH RECURSIVE r AS
(
SELECT * FROM jc_rankdiagram WHERE class_id = p_class_id AND department_id = p_dept_id
UNION ALL
SELECT jc_rankdiagram.* FROM jc_rankdiagram, r WHERE jc_rankdiagram.parent_id = r.department_id
)
SELECT r.department_id FROM r group by department_id;
END IF;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000
数据库系列:postgresql创建函数语法示例
最新推荐文章于 2024-09-03 13:30:00 发布