首页 > 开发 > 综合 > 正文

字符集问题的初步探讨(五)

2024-07-21 02:07:29
字体:
来源:转载
供稿:网友

原文链接:


http://www.eygle.com/special/nls_character_set_05.htm


原文发表于itpub技术丛书《oracle数据库dba专题技术精粹》,未经许可,严禁转载本文.

我们知道在导出文件中,记录着导出使用的字符集id,通过查看导出文件头的第2、3个字节,我们可以找到16进制表示的字符集id,在windows上,
我们可以使用ultraedit等工具打开dmp文件,查看其导出字符集::



在unix上我们可以通过以下命令来查看:

cat expdat.dmp | od -x | head



oracle提供标准函数,对字符集名称及id进行转换:

 

 sql> select nls_charset_id('zhs16gbk') from dual;nls_charset_id('zhs16gbk')-------------------------- 8521 row selected.sql> select nls_charset_name(852) from dual;nls_char--------zhs16gbk1 row selected.十进制转换十六进制:sql> select to_char('852','xxxx') from dual;to_ch----- 3541 row selected.

对应上面的图中第2、3字节,我们知道该导出文件字符集为zhs16gbk.


查询数据库中有效的字符集可以使用以下脚本:


 

 col nls_charset_id for 9999col nls_charset_name for a30col hex_id for a20select nls_charset_id(value) nls_charset_id, value nls_charset_name,to_char(nls_charset_id(value),'xxxx') hex_idfrom v$nls_valid_valueswhere parameter = 'characterset'order by nls_charset_id(value)/

输出样例如下:

 

 nls_charset_id nls_charset_name hex_id
-------------- ------------------------------ -------------
1 us7ascii 1
2 we8dec 2
3 we8hp 3
4 us8pc437 4
5 we8ebcdic37 5
6 we8ebcdic500 6
7 we8ebcdic1140 7
8 we8ebcdic285 8
...................
850 zhs16cgb231280 352
851 zhs16maccgb231280 353
852 zhs16gbk 354
853 zhs16dbcs 355
860 zht32euc 35c
861 zht32sops 35d
862 zht16dbt 35e
863 zht32tris 35f
864 zht16dbcs 360
865 zht16big5 361
866 zht16ccdc 362
867 zht16mswin950 363
868 zht16hkscs 364
870 al24utffss 366
871 utf8 367
872 utfe 368

..................................




在很多时候,当我们进行导入操作的时候,已经离开了源数据库,这时如果目标数据库的字符集和导出文件不一致,很多时候就需要进行特殊处理,
以下介绍几种方法,主要以us7ascii和zhs16gbk为例

1. 源数据库字符集为us7ascii,导出文件字符集为us7ascii或zhs16gbk,目标数据库字符集为zhs16gbk
在oracle92中,我们发现对于这种情况,不论怎样处理,这个导出文件都无法正确导入到oracle9i数据库中,这可能是因为oracle9i的编码方案发生了较大改变。

以下是我们所做的简单测试,其中导出文件命名规则为:


s-server ,后跟server字符集
c-client , 后跟导出操作时客户端字符集


导入时客户端字符集设置在命令行完成,限于篇幅,我们省略了部分测试过程。
对于oracle9ir2,我们的测试结果是us7ascii字符集,不管怎样转换,都无法正确导入zhs16gbk字符集的数据库中。

在进行导入操作时,如果字符不能正常转换,oracle数据库会自动用一个”?”代替,也就是编码63。


 

 e:/nls2>set nls_lang=american_america.us7asciie:/nls2>imp eygle/eygle file=sus7ascii-cus7ascii.dmp fromuser=eygle touser=eygle tables=testimport: release 9.2.0.4.0 - production on mon nov 3 17:14:39 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to: oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionexport file created by export:v09.02.00 via conventional pathimport done in us7ascii character set and al16utf16 nchar character setimport server uses zhs16gbk character set (possible charset conversion). . importing table "test" 2 rows importedimport terminated successfully without warnings.e:/nls2>sqlplus eygle/eyglesql*plus: release 9.2.0.4.0 - production on mon nov 3 17:14:50 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to:oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionsql> select name,dump(name) from test;namedump(name)-----------------------------????typ=1 len=4: 63,63,63,63testtyp=1 len=4: 116,101,115,1162 rows selected.sql> exitdisconnected from oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productione:/nls2>set nls_lang=american_america.zhs16gbke:/nls2>imp eygle/eygle file=sus7ascii-cus7ascii.dmp fromuser=eygle touser=eygle tables=test ignore=yimport: release 9.2.0.4.0 - production on mon nov 3 17:15:28 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to: oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionexport file created by export:v09.02.00 via conventional pathimport done in zhs16gbk character set and al16utf16 nchar character setexport client uses us7ascii character set (possible charset conversion). . importing table "test" 2 rows importedimport terminated successfully without warnings.e:/nls2>sqlplus eygle/eyglesql*plus: release 9.2.0.4.0 - production on mon nov 3 17:15:34 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to:oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionsql> select name,dump(name) from test;namedump(name)--------------------------------------------------------------------------------????typ=1 len=4: 63,63,63,63testtyp=1 len=4: 116,101,115,116????typ=1 len=4: 63,63,63,63testtyp=1 len=4: 116,101,115,1164 rows selected.sql> drop table test;table dropped.sql> exitdisconnected from oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productione:/nls2>set nls_lang=american_america.zhs16gbke:/nls2>imp eygle/eygle file=sus7ascii-czhs16gbk.dmp fromuser=eygle touser=eygle tables=test ignore=yimport: release 9.2.0.4.0 - production on mon nov 3 17:17:21 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to: oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionexport file created by export:v09.02.00 via conventional pathimport done in zhs16gbk character set and al16utf16 nchar character set. . importing table "test" 2 rows importedimport terminated successfully without warnings.e:/nls2>sqlplus eygle/eyglesql*plus: release 9.2.0.4.0 - production on mon nov 3 17:17:30 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to:oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionsql> select name,dump(name) from test;namedump(name)----------------------------------------------????typ=1 len=4: 63,63,63,63testtyp=1 len=4: 116,101,115,1162 rows selected.sql> exitdisconnected from oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productione:/nls2>set nls_lang=american_america.us7asciie:/nls2>imp eygle/eygle file=sus7ascii-czhs16gbk.dmp fromuser=eygle touser=eygle tables=test ignore=yimport: release 9.2.0.4.0 - production on mon nov 3 17:18:00 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to: oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionexport file created by export:v09.02.00 via conventional pathimport done in us7ascii character set and al16utf16 nchar character setimport server uses zhs16gbk character set (possible charset conversion)export client uses zhs16gbk character set (possible charset conversion). . importing table "test" 2 rows importedimport terminated successfully without warnings.e:/nls2>sqlplus eygle/eyglesql*plus: release 9.2.0.4.0 - production on mon nov 3 17:18:08 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to:oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionsql> select name,dump(name) from test;namedump(name)----------------------------------------????typ=1 len=4: 63,63,63,63testtyp=1 len=4: 116,101,115,116????typ=1 len=4: 63,63,63,63testtyp=1 len=4: 116,101,115,1164 rows selected.sql>

对于这种情况,我们可以通过使用oracle8i的导出工具,设置导出字符集为us7ascii,导出后修改第二、三字符,修改 0001 为
0354,这样就可以将us7ascii字符集的数据正确导入到zhs16gbk的数据库中。

修改导出文件:




导入修改后的导出文件:

 

 e:/nls2>set nls_lang=american_america.zhs16gbke:/nls2>imp eygle/eygle file=sus7ascii-cus7ascii-exp817.dmp fromuser=eygle touser=eygle tables=testimport: release 9.2.0.4.0 - production on mon nov 3 17:37:17 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to: oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionexport file created by export:v08.01.07 via conventional pathimport done in zhs16gbk character set and al16utf16 nchar character setexport server uses utf8 nchar character set (possible ncharset conversion). . importing table "test" 2 rows importedimport terminated successfully without warnings.e:/nls2>sqlplus eygle/eyglesql*plus: release 9.2.0.4.0 - production on mon nov 3 17:37:23 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to:oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionsql> select name,dump(name) from test;name dump(name)--------------------------------------------------------------------------------测试 typ=1 len=4: 178,226,202,212test typ=1 len=4: 116,101,115,1162 rows selected.sql>

2. 使用create database的方法
如果导出文件使用的字符集是us7ascii,目标数据库的字符集是zhs16gbk,我们可以使用create database的方法来修改,具体如下:


 

 sql> col parameter for a30sql> col value for a30sql> select * from v$nls_parameters;parameter value------------------------------ ------------------------------nls_language americannls_territory americanls_currency $nls_iso_currency americanls_numeric_characters .,nls_calendar gregoriannls_date_format dd-mon-rrnls_date_language americannls_characterset zhs16gbknls_sort binary……………….19 rows selected.sql> create database character set us7ascii;create database character set us7ascii*error at line 1:ora-01031: insufficient privilegessql> select * from v$nls_parameters;parameter value------------------------------ ------------------------------nls_language americannls_territory americanls_currency $nls_iso_currency americanls_numeric_characters .,nls_calendar gregoriannls_date_format dd-mon-rrnls_date_language americannls_characterset us7asciinls_sort binary…………..19 rows selected.sql> exitdisconnected from oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productione:/nls2>set nls_lang=american_america.us7asciie:/nls2>imp eygle/eygle file=sus7ascii-cus7ascii.dmp fromuser=eygle touser=eygleimport: release 9.2.0.4.0 - production on sun nov 2 14:53:26 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to: oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionexport file created by export:v09.02.00 via conventional pathimport done in us7ascii character set and al16utf16 nchar character setimport server uses zhs16gbk character set (possible charset conversion). . importing table "test" 2 rows importedimport terminated successfully without warnings.e:/nls2>sqlplus eygle/eyglesql*plus: release 9.2.0.4.0 - production on sun nov 2 14:53:35 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to:oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionsql> select * from test;name----------测试test2 rows selected.

我们看到,当发出create database character set us7ascii;命令时,数据库v$nls_parameters中的字符集设置随之更改,该参数影响导入进程,
更改后可以正确导入数据,重起数据库后,该设置恢复。

提示:v$nls_paraemters来源于x$nls_parameters,该动态性能视图影响导入操作;而nls_database_parameters来源于props$数据表,影响数据存储。

3. oracle提供的字符扫描工具csscan

我们说以上的方法只是应该在不得已的情况下使用,其本质是欺骗数据库,强制导入数据,可能损失元数据。
如果要确保数据的完整性,应该使用csscan扫描数据库,找出所有不兼容的字符,然后通过编写相应的脚本及代码,在转换之后进行更新,确保数据的正确性。
我们简单看一下csscan的使用。

要使用csscan之前,需要以sys用户身份创建相应数据字典对象:


 

 e:/nls2>sqlplus "/ as sysdba"sql*plus: release 9.2.0.4.0 - production on sun nov 2 19:42:07 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to:oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionsql> select instance_name from v$intance;select instance_name from v$intance *error at line 1:ora-00942: table or view does not existsql> select instance_name from v$instance;instance_name----------------penny1 row selected.sql> @?/rdbms/admin/csminst.sqluser created.grant succeeded.………..

这个脚本创建相应用户(csmig)及数据字典对象,扫描信息会记录在相应的数据字典表里。

我们可以在命令行调用这个工具对数据库进行扫描:

 e:/nls2>csscan full=y fromchar=zhs16gbk tochar=us7ascii log=us7check.log capture=y array=1000000 process=2character set scanner v1.1 : release 9.2.0.1.0 - production on sun nov 2 20:24:45 2003copyright (c) 1982, 2002, oracle corporation. all rights reserved.username: eygle/eygleconnected to:oracle9i enterprise edition release 9.2.0.4.0 - productionwith the partitioning, oracle label security, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionenumerating tables to scan.... process 1 scanning sys.source$[aaaabhaabaaaairaaa]. process 2 scanning sys.attribute$[aaaaeoaabaaaahzaaa]. process 2 scanning sys.parameter$[aaaaeoaabaaaahzaaa]. process 2 scanning sys.method$[aaaaeoaabaaaahzaaa]……... process 2 scanning system.def$_aqerror[aaaa8faabaaacwjaaa]. process 1 scanning wmsys.wm$env_vars[aaabewaabaaafmzaaa]………………….. process 2 scanning sys.ugroup$[aaaaa5aabaaaagpaaa]. process 2 scanning sys.con$[aaaaacaabaaaacpaaa]. process 1 scanning sys.file$[aaaaaraabaaaabxaaa]creating database scan summary report...creating individual exception report...scanner terminated successfully.

然后我们可以检查输出的日志来查看数据库扫描情况:

 database scan individual exception report[database scan parameters]parameter value ------------------------------ ------------------------------------------------scan type full database scan char data? yes current database character set zhs16gbk new database character set us7ascii scan nchar data? no array fetch buffer size 1000000 number of processes 2 capture convertible data? yes ------------------------------ ------------------------------------------------[data dictionary individual exceptions][application data individual exceptions]user : eygletable : testcolumn: nametype : varchar2(10)number of exceptions : 1 max post conversion data size: 4 rowid exception type size cell data(first 30 bytes) ------------------ ------------------ ----- ------------------------------aaabpiaadaaaaamaaa lossy conversion 测试 ------------------ ------------------ ----- ------------------------------

 

不能转换的数据将会被记录下来,我们可以根据这些信息在转换之后,对数据进行相应的更新,确保转换无误。

 

最大的网站源码资源下载站,

上一篇:初学触发器

下一篇:物料编码的原则

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表