一、效果展示 select cascadepro('北京市,西城区,西直门', 2); 结果:北京市,西直门 二、存储过程 -- 查询:select cascadepro('北京市,西城区,西直门', 2); -- 输出:北京市,西直门 create function cascadepro(value1 text, no integer) returns character varying language plpgsql as $$ DECLARE DECLARE v_value_in TEXT; v_result TEXT := ''; i_index int4 := 0; v_value TEXT; BEGIN v_value_in:= value1; IF value1 = NULL THEN RETURN NULL; END IF; IF SUBSTR( value1, 1, 1 ) = ',' THEN v_value_in = SUBSTR( value1, 2 ); END IF; IF SUBSTR( v_value_in, LENGTH ( v_value_in ), 1 ) = ',' THEN v_value_in = SUBSTR( v_value_in, 1, LENGTH ( v_value_in ) - 1 ); END IF; FOR v_value IN ( SELECT UNNEST ( string_to_array( v_value_in, ',' )) AS ID ) LOOP i_index := i_index + 1; IF i_index <= "no" THEN IF v_result = '' THEN v_result = v_value; ELSE v_result = v_result || ',' || v_value; END IF; ELSE EXIT; END IF; END LOOP; IF i_index < "no" THEN v_result = NULL; END IF; RETURN v_result; END; $$;