Oracle 仿 mysql的group_concat()函数

CREATE OR REPLACE FUNCTION F_CONCAT_ROW_COMMUNITY_LABEL(p_field     IN VARCHAR2,
                                                        p_parent_id IN VARCHAR2,
                                                        p_splicing  IN VARCHAR2)
  RETURN VARCHAR2 IS
  retResult VARCHAR2(32767);
  TYPE cur_type IS REF CURSOR;
  rscur cur_type;
  rsVal VARCHAR2(2000);
  cnt   INTEGER;
BEGIN
  cnt := 0;
  --"select distinct " || p_field || " from community_label cl join bkm_relation br on(br.child_id = cl.id) where br.relation_type = '2' and br.parent_id in (  select v.id  from v_community_knowledge v where v.deleted_flag = '0' and v.root_id = (select v2.root_id from v_community_knowledge v2 where v2.id = " || p_parent_id || "  ) )";

  --dbms_output.put_line('select ' || p_field || ' from ' || p_tableName || ' where id in (select child_id from bkm_relation where parent_id = ''' || p_parent_id || ''' and relation_type = ''' || p_relation_type || ''')');   

--上面的是注释

  OPEN rscur FOR 'select distinct cl.' || p_field || ' from community_label cl join bkm_relation br on(br.child_id = cl.id)
                  where br.relation_type = ''2'' and br.parent_id in (
                        select v.id  from v_community_knowledge v
                               where v.deleted_flag = ''0'' and v.root_id = (
                                     select v2.root_id from v_community_knowledge v2
                                            where v2.id =''' || p_parent_id || ''' ))';



  LOOP
    FETCH rscur
      INTO rsVal;
    EXIT WHEN rscur%NOTFOUND;
    IF cnt > 0 THEN
      retResult := retResult || p_splicing;
    END IF;
    retResult := retResult || rsVal;
    cnt       := cnt + 1;
  END LOOP;
  CLOSE rscur;
  return retResult;
EXCEPTION
  -- exception handlers begin
  WHEN VALUE_ERROR THEN
    --
    retResult := '';
    RETURN retResult;
END F_CONCAT_ROW_COMMUNITY_LABEL;
/**
  * example: select F_CONCAT_ROW('id','community_kn_catagory','3',id,',') from community_knowledge;
  * 2013/3/11
  **/

你可能感兴趣的:(Oracle 仿 mysql的group_concat()函数)