替换电子邮件域
Replacing Email Domains
我正在尝试将输入的域名更改为新域名,例如"hotmail.com" 变为"outlook.com"。我相信我在正确的轨道上,只是我的子串不对。
CREATE OR REPLACE PROCEDURE PR_ChangeDomain
(P_Email_Address varchar2)
IS
Cursor C_Email IS
Select Email_Address, Broker_Number
From Customer
Where Email_Address = P_Email_Address;
V_Email varchar2(50);
BEGIN
Open C_Email;
Fetch C_Email into V_Email;
While C_Email%FOUND LOOP
Update Customer
Set Email_Address = SUBSTR((Email_Address), V_Email)
Where Email_Address = P_Email_Address;
Fetch C_Email into V_Email;
End Loop;
Close C_Email;
End PR_ChangeDomain;
/
你写的代码没有多大意义;太多的获取是行不通的(两列到一个变量中?)。
这是一个例子:测试 table:
SQL> create table test (email varchar2(30));
Table created.
SQL> insert into test
2 select 'lf@hotmail.com' from dual union all
3 select 'bigfoot@net.hr' from dual union all
4 select 'stack@gmail.com' from dual union all
5 select 'overflow@gmail.com' from dual;
4 rows created.
如何从中拆分域部分(以下第 2 列 SELECT)并将电子邮件地址更新为新域(第 3 列):
SQL> select email,
2 substr(email, instr(email, '@') + 1) domain,
3 replace(email,
4 substr(email, instr(email, '@') + 1),
5 'new_domain.com'
6 ) result
7 from test;
EMAIL DOMAIN RESULT
------------------------- --------------- -------------------------
lf@hotmail.com hotmail.com lf@new_domain.com
bigfoot@net.hr net.hr bigfoot@new_domain.com
stack@gmail.com gmail.com stack@new_domain.com
overflow@gmail.com gmail.com overflow@new_domain.com
SQL>
我们只将 Gmail 电子邮件地址更新到新域:
SQL> update test set
2 email = replace(email,
3 substr(email, instr(email, '@') + 1),
4 'new_domain.com'
5 )
6 where substr(email, instr(email, '@') + 1) = 'gmail.com';
2 rows updated.
SQL> select * From test;
EMAIL
-------------------------
lf@hotmail.com
bigfoot@net.hr
stack@new_domain.com
overflow@new_domain.com
SQL>
如果你想把它转换成程序,没问题:
SQL> rollback;
Rollback complete.
SQL> create or replace procedure p_change_domain
2 (par_old_domain in varchar2,
3 par_new_domain in varchar2)
4 is
5 begin
6 update test set
7 email = replace(email,
8 substr(email, instr(email, '@') + 1),
9 par_new_domain
10 )
11 where substr(email, instr(email, '@') + 1) = par_old_domain;
12 end;
13 /
Procedure created.
SQL> exec p_change_domain('gmail.com', 'new_domain_2.com');
PL/SQL procedure successfully completed.
SQL> select * From test;
EMAIL
-------------------------
lf@hotmail.com
bigfoot@net.hr
stack@new_domain_2.com
overflow@new_domain_2.com
SQL>
如果你非常想使用游标(我不知道你为什么要这样做;这可能是最低效的选择),给你:
SQL> rollback;
Rollback complete.
SQL> create or replace procedure p_change_domain
2 (par_old_domain in varchar2,
3 par_new_domain in varchar2)
4 is
5 begin
6 for cur_r in (select email from test
7 where substr(email, instr(email, '@') + 1) = par_old_domain
8 )
9 loop
10 update test set
11 email = replace(email,
12 substr(email, instr(email, '@') + 1),
13 par_new_domain
14 )
15 where email = cur_r.email;
16 end loop;
17 end;
18 /
Procedure created.
SQL> exec p_change_domain('gmail.com', 'new_domain_3.com');
PL/SQL procedure successfully completed.
SQL> select * From test;
EMAIL
-------------------------
lf@hotmail.com
bigfoot@net.hr
stack@new_domain_3.com
overflow@new_domain_3.com
SQL>
游标 FOR 循环比您的尝试更容易维护(创建游标和游标变量、打开游标、从中获取数据、注意退出循环、关闭游标)。
但是,如果你离不开它,那就来吧:
SQL> rollback;
Rollback complete.
SQL> create or replace procedure p_change_domain
2 (par_old_domain in varchar2,
3 par_new_domain in varchar2)
4 is
5 cursor c1 is
6 select email from test
7 where substr(email, instr(email, '@') + 1) = par_old_domain;
8 c1r c1%rowtype;
9 begin
10 open c1;
11 loop
12 fetch c1 into c1r;
13 exit when c1%notfound;
14
15 update test set
16 email = replace(email,
17 substr(email, instr(email, '@') + 1),
18 par_new_domain
19 )
20 where email = c1r.email;
21 end loop;
22 close c1;
23 end;
24 /
Procedure created.
SQL> exec p_change_domain('gmail.com', 'new_domain_4.com');
PL/SQL procedure successfully completed.
SQL> select * From test;
EMAIL
-------------------------
lf@hotmail.com
bigfoot@net.hr
stack@new_domain_4.com
overflow@new_domain_4.com
SQL>
我的建议?如果可能,请使用纯 SQL。或第一个 PL/SQL 过程。不要为此目的使用游标。
我正在尝试将输入的域名更改为新域名,例如"hotmail.com" 变为"outlook.com"。我相信我在正确的轨道上,只是我的子串不对。
CREATE OR REPLACE PROCEDURE PR_ChangeDomain
(P_Email_Address varchar2)
IS
Cursor C_Email IS
Select Email_Address, Broker_Number
From Customer
Where Email_Address = P_Email_Address;
V_Email varchar2(50);
BEGIN
Open C_Email;
Fetch C_Email into V_Email;
While C_Email%FOUND LOOP
Update Customer
Set Email_Address = SUBSTR((Email_Address), V_Email)
Where Email_Address = P_Email_Address;
Fetch C_Email into V_Email;
End Loop;
Close C_Email;
End PR_ChangeDomain;
/
你写的代码没有多大意义;太多的获取是行不通的(两列到一个变量中?)。
这是一个例子:测试 table:
SQL> create table test (email varchar2(30));
Table created.
SQL> insert into test
2 select 'lf@hotmail.com' from dual union all
3 select 'bigfoot@net.hr' from dual union all
4 select 'stack@gmail.com' from dual union all
5 select 'overflow@gmail.com' from dual;
4 rows created.
如何从中拆分域部分(以下第 2 列 SELECT)并将电子邮件地址更新为新域(第 3 列):
SQL> select email,
2 substr(email, instr(email, '@') + 1) domain,
3 replace(email,
4 substr(email, instr(email, '@') + 1),
5 'new_domain.com'
6 ) result
7 from test;
EMAIL DOMAIN RESULT
------------------------- --------------- -------------------------
lf@hotmail.com hotmail.com lf@new_domain.com
bigfoot@net.hr net.hr bigfoot@new_domain.com
stack@gmail.com gmail.com stack@new_domain.com
overflow@gmail.com gmail.com overflow@new_domain.com
SQL>
我们只将 Gmail 电子邮件地址更新到新域:
SQL> update test set
2 email = replace(email,
3 substr(email, instr(email, '@') + 1),
4 'new_domain.com'
5 )
6 where substr(email, instr(email, '@') + 1) = 'gmail.com';
2 rows updated.
SQL> select * From test;
EMAIL
-------------------------
lf@hotmail.com
bigfoot@net.hr
stack@new_domain.com
overflow@new_domain.com
SQL>
如果你想把它转换成程序,没问题:
SQL> rollback;
Rollback complete.
SQL> create or replace procedure p_change_domain
2 (par_old_domain in varchar2,
3 par_new_domain in varchar2)
4 is
5 begin
6 update test set
7 email = replace(email,
8 substr(email, instr(email, '@') + 1),
9 par_new_domain
10 )
11 where substr(email, instr(email, '@') + 1) = par_old_domain;
12 end;
13 /
Procedure created.
SQL> exec p_change_domain('gmail.com', 'new_domain_2.com');
PL/SQL procedure successfully completed.
SQL> select * From test;
EMAIL
-------------------------
lf@hotmail.com
bigfoot@net.hr
stack@new_domain_2.com
overflow@new_domain_2.com
SQL>
如果你非常想使用游标(我不知道你为什么要这样做;这可能是最低效的选择),给你:
SQL> rollback;
Rollback complete.
SQL> create or replace procedure p_change_domain
2 (par_old_domain in varchar2,
3 par_new_domain in varchar2)
4 is
5 begin
6 for cur_r in (select email from test
7 where substr(email, instr(email, '@') + 1) = par_old_domain
8 )
9 loop
10 update test set
11 email = replace(email,
12 substr(email, instr(email, '@') + 1),
13 par_new_domain
14 )
15 where email = cur_r.email;
16 end loop;
17 end;
18 /
Procedure created.
SQL> exec p_change_domain('gmail.com', 'new_domain_3.com');
PL/SQL procedure successfully completed.
SQL> select * From test;
EMAIL
-------------------------
lf@hotmail.com
bigfoot@net.hr
stack@new_domain_3.com
overflow@new_domain_3.com
SQL>
游标 FOR 循环比您的尝试更容易维护(创建游标和游标变量、打开游标、从中获取数据、注意退出循环、关闭游标)。
但是,如果你离不开它,那就来吧:
SQL> rollback;
Rollback complete.
SQL> create or replace procedure p_change_domain
2 (par_old_domain in varchar2,
3 par_new_domain in varchar2)
4 is
5 cursor c1 is
6 select email from test
7 where substr(email, instr(email, '@') + 1) = par_old_domain;
8 c1r c1%rowtype;
9 begin
10 open c1;
11 loop
12 fetch c1 into c1r;
13 exit when c1%notfound;
14
15 update test set
16 email = replace(email,
17 substr(email, instr(email, '@') + 1),
18 par_new_domain
19 )
20 where email = c1r.email;
21 end loop;
22 close c1;
23 end;
24 /
Procedure created.
SQL> exec p_change_domain('gmail.com', 'new_domain_4.com');
PL/SQL procedure successfully completed.
SQL> select * From test;
EMAIL
-------------------------
lf@hotmail.com
bigfoot@net.hr
stack@new_domain_4.com
overflow@new_domain_4.com
SQL>
我的建议?如果可能,请使用纯 SQL。或第一个 PL/SQL 过程。不要为此目的使用游标。