获取字符串并删除重复项

时间:2016-07-04 20:50:22

标签: plsql

我有一个名为cACLsonDistribution的游标,我将其提取到名为ACL的变量中。 光标中填充了电子邮件列表,有时这些电子邮件是重复的。

LOOP
      FETCH cACLsonDistribution INTO ACLs;
      EXIT WHEN cACLsonDistribution%NOTFOUND;
         ACLsonDistribution := ACLsonDistribution || ','|| ACLs.ENGINEER_EMAIL;

          END LOOP;   

这将返回如下字符串:

  

,bernardo.t @ example.com,bernardo.t @ example.com,felipe.t @ example.com,jose.t @ example.com

正如您所看到的,那里有2个重复的电子邮件。有时会有3封重复的电子邮件。我想要返回没有重复项的字符串。像这样:

  

,bernardo.t @ example.com,felipe.t @ example.com,jose.t @ example.com

1 个答案:

答案 0 :(得分:0)

试试这个简单的插图。希望这会有所帮助。

SET SQLBL ON;
SET serveroutput ON;
DECLARE
  PLST SYS_REFCURSOR;
  AV      VARCHAR2(100);
  LV_VAR  VARCHAR2(100);
  VAR_TAB VARCHAR2(100);
BEGIN
  OPEN PLST FOR SELECT 'avrajit.roy@abc.com' nm FROM DUAL
  UNION ALL
  SELECT 'avrajit.roy@abc.com' nm FROM DUAL
  UNION ALL
  SELECT 'shubhojit.roy@abc.com' nm FROM DUAL;
  LOOP
    FETCH plst INTO AV;
    EXIT
  WHEN PLST%NOTFOUND;
    --  NULL;
    lv_var:=lv_var||','||av;
  END LOOP;
  --  close
  DBMS_OUTPUT.PUT_LINE(SUBSTR(LV_VAR,2,LENGTH(LV_VAR)));
  SELECT WMSYS.WM_CONCAT(a.txt)
  INTO var_tab
  FROM
    ( SELECT DISTINCT(REGEXP_SUBSTR(SUBSTR(LV_VAR,2,LENGTH(LV_VAR)),'[^,]+', 1, level)) txt
    FROM DUAL
      CONNECT BY regexp_substr(SUBSTR(LV_VAR,2,LENGTH(LV_VAR)), '[^,]+', 1, level) IS NOT NULL
    )a;
  DBMS_OUTPUT.PUT_LINE(var_tab);
END;

----------------------------------OUTPUT----------------------------------------

avrajit.roy@abc.com,shubhojit.roy@abc.com

----------------------------------OUTPUT----------------------------------------