Kode yang Anda tulis tidak masuk akal; terlalu banyak pengambilan yang tidak akan berhasil (dua kolom menjadi satu variabel?).
Berikut ini contohnya:tabel pengujian:
SQL> create table test (email varchar2(30));
Table created.
SQL> insert into test
2 select '[email protected]' from dual union all
3 select '[email protected]' from dual union all
4 select '[email protected]' from dual union all
5 select '[email protected]' from dual;
4 rows created.
Cara memisahkan bagian domain darinya (kolom ke-2 dari SELECT berikut) dan memperbarui alamat email ke domain baru (kolom ke-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
------------------------- --------------- -------------------------
[email protected] hotmail.com [email protected]_domain.com
[email protected] net.hr [email protected]_domain.com
[email protected] gmail.com [email protected]_domain.com
[email protected] gmail.com [email protected]_domain.com
SQL>
Mari perbarui hanya alamat email Gmail ke domain baru:
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
-------------------------
[email protected]
[email protected]
[email protected]_domain.com
[email protected]_domain.com
SQL>
Jika Anda ingin mengubahnya menjadi prosedur, tidak masalah:
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
-------------------------
[email protected]
[email protected]
[email protected]_domain_2.com
[email protected]_domain_2.com
SQL>
Jika Anda sangat ingin menggunakan kursor (saya tidak tahu mengapa Anda ingin melakukannya; itu mungkin opsi yang paling tidak efisien), ini dia:
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
-------------------------
[email protected]
[email protected]
[email protected]_domain_3.com
[email protected]_domain_3.com
SQL>
Kursor FOR loop lebih mudah dipertahankan daripada upaya Anda (membuat kursor dan variabel kursor, membuka kursor, mengambilnya, berhati-hati saat keluar dari loop, menutup kursor).
Tapi, jika Anda tidak bisa hidup tanpanya, ini dia:
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
-------------------------
[email protected]
[email protected]
[email protected]_domain_4.com
[email protected]_domain_4.com
SQL>
Saran saya? Gunakan SQL murni, jika memungkinkan. Atau prosedur PL/SQL pertama. Jangan gunakan kursor untuk tujuan ini.