Oracle 10中修改字符集(character set)

Oracle 10.2.0.3数据库原来的字符集是ZHS16GBK,为了支持更多的汉字,需要修改为ZHS32GB18030。

我首先想到原来在9i上修改字符集的方法,过程如下:

sys@CNHTM> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
sys@CNHTM> startup mount
ORACLE instance started.

Total System Global Area 167772160 bytes
Fixed Size 1218292 bytes
Variable Size 79694092 bytes
Database Buffers 79691776 bytes
Redo Buffers 7168000 bytes
Database mounted.
sys@CNHTM> alter system set job_queue_processes=0;

System altered.

sys@CNHTM> alter system set aq_tm_processes=0;

System altered.

sys@CNHTM> alter system enable restricted session;

System altered.

sys@CNHTM> alter database open;

Database altered.

sys@CNHTM> alter database character set ZHS32GB18030;
alter database character set ZHS32GB18030
*
ERROR at line 1:
ORA-12712: new character set must be a superset of old character set

晕啊,ZHS32GB18030字符集明明是ZHS16GBK的超集,查metalink才知道,原来在Oracle 10G中,修改字符集需要用csalter配合csscan完成。过程如下:

% cd $ORACLE_HOME/rdbms/admin
% sqlplus sys/password as sysdba
SQL> START csminst.sql

安装csscan

oracle@oracle[/home/oracle]> sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Wed Dec 23 19:58:21 2009

Copyright (c) 1982, 2005, Oracle. All rights reserved.


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


sys@CNHTM> set termout on
sys@CNHTM> set echo on
sys@CNHTM> spool /tmp/csminst.log
sys@CNHTM> @?/rdbms/admin/csminst.sql
sys@CNHTM> rem
sys@CNHTM> rem NAME
sys@CNHTM> rem csminst.sql

... 省略n行 ...

sys@CNHTM> rem *****************************************************************
sys@CNHTM> rem CSMV$EXTABLES lists all distinct objects to be scaned
sys@CNHTM> rem *****************************************************************
sys@CNHTM> create or replace view csmig.csmv$extables
2 (obj#, usr#, property)
3 as
4 select distinct(obj#), usr#, property
5 from csm$extables where property=0;

View created.

sys@CNHTM> /

View created.

sys@CNHTM> commit
2 /

Commit complete.

sys@CNHTM> exit;
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

安装后会自动退出plsql,可以检查/tmp/csminst.log文件,查看安装的详细日志。

测试csscan是否安装成功

oracle@oracle[/home/oracle]> csscan table=sys.sql_version$ fromchar=zhs16gbk tochar=zhs32gb18030 log=instchkc capture=n process=1 array=1024000


Character Set Scanner v2.1 : Release 10.2.0.0.0 - Production on Wed Dec 23 20:15:30 2009

Copyright (c) 1982, 2005, Oracle. All rights reserved.


Username: / as sysdba (这里输入用户名密码)

Password:

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

Enumerating table to scan...

. process 1 scanning SYS.SQL_VERSION$[AAAAIDAABAAAA/xAAA]

Creating Database Scan Summary Report...

Creating Individual Exception Report...

Scanner terminated successfully.

如果显示Scanner terminated successfully.证明csscan安装成功,会生成如下三个文件。把这三个文件删除。

oracle@oracle[/home/oracle]> ls -l instchkc*
-rw-r--r-- 1 oracle oinstall 1357 Dec 23 20:16 instchkc.err
-rw-r--r-- 1 oracle oinstall 504 Dec 23 20:16 instchkc.out
-rw-r--r-- 1 oracle oinstall 5481 Dec 23 20:16 instchkc.txt

开始正式运行csscan,下一步的csalter过程依赖这一步的运行结果。

oracle@oracle[/home/oracle]> csscan full=y tochar=zhs32gb18030 array=1024000 process=2


Character Set Scanner v2.1 : Release 10.2.0.0.0 - Production on Wed Dec 23 20:21:14 2009

Copyright (c) 1982, 2005, Oracle. All rights reserved.


Username: / as sysdba (这里输入用户名密码)

Password:

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

Enumerating tables to scan...

. process 1 scanning SYS.SOURCE$[AAAABIAABAAAAIRAAA]
. process 2 scanning SYS.TYPE$[AAAAC1AABAAAAURAAA]


...省略n行...


. process 2 scanning SYS.WRH$_INST_CACHE_TRANSFER[AAAMgoAADAAABNhAAA]
. process 1 scanning SYSTEM.LOGMNRC_GTLO[AAABa0AADAAAAZhAAA]
. process 2 scanning SYS.WRH$_DLM_MISC[AAAMhlAADAAAA5RAAA]

Creating Database Scan Summary Report...

Creating Individual Exception Report...

Scanner terminated successfully.

运行csalter,修改字符集

oracle@oracle[/home/oracle]> sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Wed Dec 23 20:34:12 2009

Copyright (c) 1982, 2005, Oracle. All rights reserved.


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

sys@CNHTM> @?/rdbms/admin/csalter.plb

4 rows created.


Function created.


Function created.


Procedure created.

This script will update the content of the Oracle Data Dictionary.
Please ensure you have a full backup before initiating this procedure.
Would you like to proceed (Y/N)?y
old 6: if (UPPER('&conf') <> 'Y') then
new 6: if (UPPER('y') <> 'Y') then
Checking data validility...
begin converting system objects

PL/SQL procedure successfully completed.

Alter the database character set...
CSALTER operation completed, please restart database

PL/SQL procedure successfully completed.


4 rows deleted.


Function dropped.


Function dropped.


Procedure dropped.

重启数据库

sys@CNHTM> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
sys@CNHTM> startup
ORACLE instance started.

Total System Global Area 167772160 bytes
Fixed Size 1218292 bytes
Variable Size 71305484 bytes
Database Buffers 88080384 bytes
Redo Buffers 7168000 bytes
Database mounted.
Database opened.

检查字符集

sys@CNHTM> select userenv('language') from dual;

USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.ZHS32GB18030

结果显示,修改成功

--end--
================================================================================================

运行csalter修改字符集;

对于RAC环境:

1. 修改CLUSTER_DATABASE参数为flase

Alter system set cluster_database=flase scope=spfile

Alter system set job_queue_processes=0 scope=spfile;

alter system set aq_tm_processes=0  scope=spfile;

2. 启动数据库到单实例模式;

startuprestrict

spool switch.log

3. 运行csalter

---- sys执行:@ ?/rdbms/admin/csalter.plb

---- 检查执行csalter的输出信息,是否有错误;

如果执行那么修改还原原始的参数:

Alter system set cluster_database=true scope=spfile

Alter system set job_queue_processes=old_value scope=both sid=*;

alter system set aq_tm_processes=old_value scope=both sid=*;

4. 重启数据库;

----shutdown immediate

----startup

----检查字符集是否修改成功

检查alert log或运行如下sql

select userenv('language') from dual;

================================================================================================
安装配置csscan工具;
用具有dba权限的用户执行@?/rdbms/admin/csminst.sql;
运行csscan进行扫描;
例如:
csscan system/oracle full=y FROMCHAR=WE8ISO8859P1TOCHAR=ZHS16GBK ARRAY=1024000 PROCESS=1
可以csscan help=y查看帮助
Csscan运行结束后,默认会在当前目前下生存如下3个文件:
[oracle@roger oracle]$ ls -ltr scan*
-rw-r--r--  1oracle dba  8239 Jul  4 16:56 scan.txt
-rw-r--r--  1oracle dba 73078 Jul  4 16:56 scan.out
-rw-r--r--  1oracle dba  1878 Jul  4 16:56 scan.err
 
Scan工具会把最近一次扫描的参数写入到同义词csm$parameters中,下次进行数据库字符集转换时,直接从该同义词读取信息;
检查scan.txt:
对于convertible, truncated的对象可以通过exp/imp来完成;
对于lossy的对象,可以用plsql进行导出然后手工转换编码,然后删除对象,等运行csalter命令修改字符集完成后,再将前面的对象导入。

导出convetitble和truncate的对象后,需要重新运行csscan工具;

================================================================================================

1.首先执行$ORACLE_HOME/rdbms/admin/csminst.sql,创建一些数据库对象,包括数据库用户
2.备份Oracle数据库,最好全库备份;
3.使用csscan命令检查字符集转换是否可行.csscan有四种扫描模式:全库扫描,按用户扫描,按表扫描,按列扫描.因为我们是改变全库的字符集,所以要用全库扫描.并且要求有DBA权限.
4.运行csscan扫描数据库:csscan SYSTEM/testdb FULL=y TOCHAR=ZHS16GBK ARRAY=1024000 PROCESS=1
5.扫描结束后,会在当前目录下产生三个文件:scan.txt,scan.out,scan.err,我们更多的需要关注scan.err
6.可能需要多运行几次csscan命令,以确保scan.err中没有报错,才能进行字符转换.
7.没有问题之后,然后运行$ORACLE_HOME/rdbms/admin/csalter.plb脚本,进行转化。转化的过程中,需要注意,数据库不能有别的session运行,否则会报错.另外,由于在转换的过程中读取的是CSMIG用户下的CSM$PARAMETERS表中的数据,因此在转换前必须用csscan执行一次全库扫描.

可能会遇到的问题:
Full database scan is required ----需要全表扫描
Exceptional data found in scanner result -----检查scan.err应该还有错误报告
Sorry only one session is allowed to run this script -----还有别的session在运行,断开连接后再试试.

你可能感兴趣的:(oracle,数据库,database,System,Parameters,character)