一.字符集编码知识
查询字符集编码:
SQL> select userenv('language') from dual;
或者 select * from V$NLS_PARAMETERS;
或者 select * from nls_database_parameters where parameter like 'NLS%CHARACTERSET';
USERENV('LANGUAGE')
----------------------------------------------------
SIMPLIFIED CHINESE_CHINA.ZHS16GBK
环境变量nls_lang便是由这三部分组成:
1. NLS_LANGUAGE
2. NLS_TERRITORY
3. NLS_CHARACTERSET
组成如下:NLS_LANG = language_territory.charset
即语言、地域和字符集,每个成分控制了 NLS 子集的特性。其中:
Language: 指定服务器消息的语言, 影响提示信息是中文还是英文
Territory: 指定服务器的日期和数字格式
Charset:指定字符集。
真正影响数据库的是第三部分,因此,我们将Charset修改为常用的AL32UTF8
二. 修改Oracle服务器端的字符集
SQL> startup nomount
SQL> alter database mount exclusive; --装载数据为专用的高级模式;
SQL> alter system enable restricted session; --启用受限制的session模式
SQL> alter system set job_queue_processes=0; --'maximum number of job queue slave processes' 设置工作队列的最大进程数为0
SQL> alter system set aq_tm_processes=0;
SQL> alter database open;
SQL> alter database character set AL32UTF8; --新的字符集必须支持旧的字符集(旧字符集的超集),
USERENV('LANGUAGE')
----------------------------------------------------
SIMPLIFIED CHINESE_CHINA.ZHS16GBK
ORA-28056: Writing audit records to Windows Event Log failed
ORA-28056: Writing audit records to Windows Event Log failed
O/S-Error: (OS 1502) 事件日志文件已满。
Cause
The Event Viewer log is full and not able to log any more events.
Justified by unpublished Bug 6823281 ORA-28056 WRITING AUDIT RECORDS TO WINDOWS EVENT LOG FAILED
which was closed as not a bug.
Solution
Try doing the following using Windows Event Viewer:
To open Event Viewer, click Start, click Control Panel,
click Performance and Maintenance, click Administrative Tools,
and then double-click Event Viewer.
You must be logged on as an administrator or a member of the
Administrators group to free an event log.
1) When a log is full, it stops recording new events. Clearing the log
is one way to free the log and start recording new events.
2) You can also free a log and start recording new events by overwriting
old events. To overwrite events, on the Action menu, click Properties,
and then click Overwrite events as needed. This ensures that all new
events are written to the log, even when the log is full.
3) You can also start logging new events by increasing the maximum
log size. To increase the log size, on the Action menu, click Properties,
and then increase the Maximum log size.
References
USERENV('LANGUAGE')
-------------------------------------------------------------------------------
SIMPLIFIED CHINESE_CHINA.AL32UTF8
三. 客户端关于Oracle的字符集编码设置
客户端与服务器端字符集不一致时可能导致cmd或者shell显示乱码:
Windows:
# 常用中文字符集
set NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK(仅在此cmd窗口立即生效)
# 常用unicode字符集
set NLS_LANG=american_america.AL32UTF8
可以通过修改注册表键值永久设置
HKEY_LOCAL_MACHINE/SOFTWARE/ORACLE/KEY_OraDb11g_home1/NLS_LANG(任何窗口生效,但是需要重启系统生效)
#通过属性-高级系统设置-环境变量加入
NLS_LANG=american_america.AL32UTF8环境变量(此环境变量优先级高于注册表环境变量)
Unix:
# 常用unicode字符集
export NLS_LANG=american_america.AL32UTF8
# 常用中文字符集
export NLS_LANG="Simplified Chinese_china".ZHS16GBK
可以编辑 bash_profile 文件进行永久设置
vi .bash_profile
NLS_LANG="Simplified Chinese_china".ZHS16GBK export NLS_LANG
# 使 bash_profile 设置生效
source .bash_profile
四. sql脚本导入Oralce服务器执行时各种字符集的测试
执行结果
对结果的分析
补充:
NLS_CHARACTERSET与NLS_NCHAR_CHARACTERSET的区别
NLS_CHARACTERSET是数据库字符集,NLS_NCHAR_CHARACTERSET是国家字符集
ORACLE中有两大类字符型数据,VARCHAR2是按照数据库字符集来存储数据。而NVARCHAR2是按照国家字符集存储数据的。同样,CHAR和NCHAR也一样,一是数据库字符符,一是国家字符集。
字符集不同,二进制码的组合就不同。
比如有一串二进制信息:1101,0110,1101,0000,1011,1001,1111,1010,按照16位双字节GBK字符集理解,可以代表“中国”两个字。如果单字节的字符集,这一串二进制代表ASC码为214、208、185、250的四个怪字符。
这就是字符集的作用,就是以什么样的形式理解信息。
The NLS_CHARACTERSET is used for CHAR, VARCHAR2, LONG and CLOB columns;
The NLS_NCHAR_CHARACTERSET is used for NCHAR, NVARCHAR2 and NCLOB columns.
From 9i onwards the NLS_NCHAR_CHARACTERSET can have only 2 values: UTF8 or AL16UTF16 who are Unicode charactersets
注:我的例子中“中国”的二进制形式来自dump('中国',16)。
Windows修改cmd字符集
http://blog.itpub.net/25744374/viewspace-753227/
oracle不同系统之间时间转换
参考:
http://www.eygle.com/archives/2004/09/nls_character_set_01.html
http://blog.youkuaiyun.com/wzy0623/article/details/1683337
http://blog.sina.com.cn/s/blog_95c27c530101ap58.html
http://bbs.51cto.com/thread-1017691-1.html