一.字符集編碼知識
查詢字符集編碼:
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.csdn.net/wzy0623/article/details/1683337
http://blog.sina.com.cn/s/blog_95c27c530101ap58.html
http://bbs.51cto.com/thread-1017691-1.html