通过dblink+utl_raw在不同字符集数据库间迁移数据~

数据库test (字符集US7ASCII)

Z:\>set ORACLE_SID=TEST

Z:\>set NLS_LANG=AMERICAN_AMERICA.US7ASCII

Z:\>sqlplus mydb

SQL*Plus: Release 10.2.0.4.0 - Production on Wed Feb 25 13:37:20 2009

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.

Enter password:

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

MYDB > show parameter db_name

NAME                                     TYPE        VALUE
---------------------------------------- ----------- --------------------------------------------------
db_name                                  string      TEST
MYDB > create table chtest (a number,b varchar2(20),c varchar2(20));

Table created.

MYDB > insert into chtest values (1,'中华人民共和国','China');

1 row created.

Elapsed: 00:00:00.15
MYDB > insert into chtest values (1,'美利坚合众国','USA');

1 row created.

Elapsed: 00:00:00.03
MYDB > insert into chtest values (1,'今天你吃了么?','XXXxxxABC');

1 row created.

Elapsed: 00:00:00.01
MYDB > insert into chtest values (10000,'字符集,测试数据','3943kdfaAC#E');

1 row created.

Elapsed: 00:00:00.03
MYDB > commit;

Commit complete.

Elapsed: 00:00:00.00
MYDB > select * from chtest;

         A B                    C
---------- -------------------- --------------------
         1 中华人民共和国       China
         1 美利坚合众国         USA
         1 今天你吃了么?       XXXxxxABC
     10000 字符集,测试数据     3943kdfaAC#E

Elapsed: 00:00:00.01

需要将表chtest迁移到数据库MYDB102(字符集ZHS16GBK)中.

直接通过dblink中文为乱码:

Z:\>set ORACLE_SID=MYDB102

Z:\>set NLS_LANG=AMERICAN_AMERICA.ZHS16GBK

Z:\>sqlplus mydb

SQL*Plus: Release 10.2.0.4.0 - Production on Wed Feb 25 13:41:03 2009

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.

Enter password:

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

MYDB > show parameter db_name

NAME                                     TYPE        VALUE
---------------------------------------- ----------- --------------------------------------------------
db_name                                  string      MYDB102
MYDB > create public database link TESTDB
  2    connect to MYDB identified by xxxxxxxx
  3    using 'TEST';

Database link created.

Elapsed: 00:00:00.54
MYDB > SELECT * FROM CHTEST@TESTDB;

         A B                                        C
---------- ---------------------------------------- ----------------------------------------
         1 ??????????????                           China
         1 ????????????                             USA
         1 ??????????????                           XXXxxxABC
     10000 ????????????????                         3943kdfaAC#E

Elapsed: 00:00:01.03

通过ult_raw来转换.

1.在TEST上建立View v_chtest

MYDB > create view v_chtest as select a,utl_raw.cast_to_raw(b) b, c from chtest;

View created.

Elapsed: 00:00:00.01

MYDB > select * from v_chtest;

         A B                                        C
---------- ---------------------------------------- --------------------
         1 D6D0BBAAC8CBC3F1B9B2BACDB9FA             China
         1 C3C0C0FBBCE1BACFD6DAB9FA                 USA
         1 BDF1CCECC4E3B3D4C1CBC3B4A3BF             XXXxxxABC
     10000 D7D6B7FBBCAFA3ACB2E2CAD4CAFDBEDD         3943kdfaAC#E

Elapsed: 00:00:00.01

2.在MYDB102上使用utl_raw来读取v_chtest

MYDB > select a ,utl_raw.cast_to_varchar2(b) b, c from v_chtest@testdb;

         A B                                        C
---------- ---------------------------------------- --------------------
         1 中华人民共和国                           China
         1 美利坚合众国                             USA
         1 今天你吃了么?                           XXXxxxABC
     10000 字符集,测试数据                         3943kdfaAC#E

Elapsed: 00:00:00.06

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/386081/viewspace-557730/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/386081/viewspace-557730/

你可能感兴趣的:(通过dblink+utl_raw在不同字符集数据库间迁移数据~)