【问题标题】:How to replace domain name of all email如何替换所有电子邮件的域名
【发布时间】:2019-03-03 20:36:57
【问题描述】:

我正在尝试用特定的电子邮件域替换任何电子邮件的结尾。该过程将传递两个字符串:第一个是要替换的原始域名;第二个是要替换的原始域名;其次是将替换原始域名的新域名(按此顺序)。例如,如果向过程传递字符串“sadme.com”和“happyme.com”,则数据库中以“sadme.com”为域名的每封电子邮件都将更改为“happyme.com”作为其域名。域名

例子:

Before                     After
-------------------------- ---------------------------
ExampleEmail@yahoo.com     ExampleEmail@gmail.com
SecondExample@hotmail.com  SecondExample@gmail.com

这是我所拥有的,但我遇到了错误

          Create Procedure PR_Q3
    (P_OldDomain Varchar2,P_NewDomain Varchar2)
    As
      Cursor C_Domains IS Select Email_Address


       From Broker 
                     where Email_Address =  '%@'||P_OldDomain;
               V_OldDomain Varchar2(50);       
          Begin
          Open C_Domains;
          Fetch C_Domains INTO V_OldDomain;
          While C_Domains%Found loop
                if C_Domains = '%@'||P_OldDomain Then
                    Update Broker
                    Set Email_Address = P_NewDomain
                    Where Email_Address = V_OldDomain;
              End if;      
          Fetch C_Domains into P_NewDomain;
          End Loop;
  Close C_Domains;
End PR_Q3;
/
Show Errors;

我得到了错误:

行/列错误


2/29 PLS-00103:在预期以下情况之一时遇到符号“(”:

     := . ) , @ % default character
     The symbol ":=" was substituted for "(" to continue.

2/61 PLS-00103:在预期以下情况之一时遇到符号“(”:

     := . ) , @ % default character
     The symbol ":=" was substituted for "(" to continue.

我还尝试取出 varchar2(50) 并放入 Varchar2,它给了我错误:

 PLS-00403: expression 'P_NewDomain' cannot be used as an INTO-target of a SELECT/FETCH statement

谢谢!

【问题讨论】:

    标签: regex oracle plsql oracle10g plsqldeveloper


    【解决方案1】:

    您不需要光标,因为 UPDATE 就足够了(除非您遗漏了更多的故事)。请记住,逐行 = 慢慢来!另外,您的创建过程代码中有一些语法错误。此更新仅选择与您的旧域条件匹配的行,并替换为传入的新域。

    Create or replace Procedure PR_Q3(P_OldDomain varchar2, P_NewDomain Varchar2)
    As
    BEGIN
      UPDATE Broker
        Set Email_Address = regexp_replace(Email_Address, '^(.*@.*)'||P_OldDomain, '\1'||P_NewDomain)
        WHERE REGEXP_LIKE(Email_Address, '.*@.*'||P_OldDomain);
      COMMIT;
    End PR_Q3;
    

    别忘了添加异常处理,我把它留给你。

    【讨论】:

    • 我至少需要一个光标,我可以用光标做到这一点并仍然使用您编写的方式吗?
    【解决方案2】:

    从位置 1 开始,将第二组不是'@' 的字符替换为bananas.com

    regexp_replace(email, '[^@]+', 'bananas.com', 1, 2)
    

    或者只是将 '@' 之后的所有内容替换为 @bananas.com

    regexp_replace(email, '@.+*','@bananas.com')
    

    【讨论】:

    • 这在有游标的过程中也能工作吗?
    • 我不明白为什么不,它只是 SQL。
    【解决方案3】:

    我在 Oracle 10 DB 上运行了以下命令。希望这是您所需要的:

    WITH
        sampledata AS
            (SELECT 'finger@doctors.com' AS eaddress FROM DUAL
             UNION ALL
             SELECT 'toe.on.foon@real.goofy.org' FROM DUAL
             UNION ALL
             SELECT 'facenose@skeleton.usa' FROM DUAL)
    SELECT eaddress
         , REGEXP_REPLACE (eaddress, '^(.+@).+(\..+$)', '\1gmail\2') t
      FROM sampledata;
    
    eaddress                     t
    finger@doctors.com          finger@gmail.com
    toe.on.foon@real.goofy.org  toe.on.foon@gmail.org
    facenose@skeleton.usa       facenose@gmail.usa
    

    括号是可以在替换字符串中引用的分组,用 \1 \2 \3 ...

    "^"        - anchor to start of string
    "(.+@)"    - take everything up to '@', we will reference this as \1
    ".+"       - any number of characters following the @ sign
    "+(\..+$)" - everything followed by the last period, anchored to the end of the string
    we will reference this with \2
    
    "\1gmail\2" - take the first reference, follow it with "gmail", then follow that 
    with the second reference
    

    【讨论】:

      猜你喜欢
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 2019-06-30
      • 2016-05-12
      • 2012-05-31
      • 2014-07-15
      相关资源
      最近更新 更多