一、NLS Parameter
Oracle called globalization support National Language Support (NLS),
可以從Session, Instance, Database三個層級來對NLS參數進行設定。
Session層級的設定覆蓋Instance層級的設定,Instance層級的設定覆蓋Database層級的設定。
相應地,Oracle提供NLS_SESSION_PARAMETERS,NLS_INSTANCE_PARAMETERS,NLS_DATABASE_PARAMETERS這3個視圖來查看NLS參數。
此外,還可以在SQL函數中指定NLS參數,這些函數稱作locale-dependent。
A) NLS_SESSION_PARAMETERS
此視圖中的參數可以通過"ALTER SESSION "語句來進行設定。
ALTER SESSION SET parameter_name=parameter_value [ parameter_name=parameter_value ]... ;
如果沒有通過"ALTER SESSION "語句設定過,那麼使用NLS_LANG環境變數的定義值。(NLS_LANG環境變數下面詳細說明)
如果NLS_LANG也沒有被定義,那麼沿用NLS_INSTANCE_PARAMETERS中的設定值。
這些參數影響到用戶端的錯誤資訊顯示語言;日期,數字,貨幣等的顯示格式;排序結果等。
B) NLS_INSTANCE_PARAMETERS
此視圖中中的參數設定是資料庫啟動時參照init.ora中的設定而來。 可以通過"ALTER SYSTEM"語句更改。
NLS_LANGUAGE設定決定了alert.log和trace files中的Server錯誤資訊的語言。
C) NLS_DATABASE_PARAMETERS
此視圖中的參數是建立資料庫時決定的。資料庫建立好以後不能改動,不要通過修改系統資料表格來修改設定。
如果INSTANCE和SESSION參數沒有設定,那麼預設使用這裡的參數。
NLS_CHARACTERSET and NLS_NCHAR_CHARACTERSET 參數是建立資料庫時決定的,不能被INSTANCE和SESSION參數覆蓋,不要在建立完資料庫後試圖修改它們,這種嘗試會損壞資料庫。(關於這2個參數下面有詳細說明)
注1:NLS_DATABASE_PARAMETERS視圖等價於SELECT name,value$ from sys.props$ where name like '%NLS%'。
注2:V$NLS_PARAMETERS視圖給出了當前的SESSION參數以及NLS_DATABASE_PARAMETERS視圖中的2個CHARACTERSET參數。
注3:select userenv ('language') from dual; select sys_context('userenv','language') from dual;這2個語句執行結果等同於
<當前Session的Language>_<當前Session的Territory> .<資料庫的字元集NLS_CHARACTERSET>,
這個結果貌似NLS_LANG,但是實際上不是,NLS_LANG定義的是用戶端的字元集。
注4:select userenv ('lang') from dual; 返回的是Session參數NLS_LANGUAGE的簡寫。
注5:V$NLS_VALID_VALUES視圖返回NLS_LANGUAGE, NLS_SORT, NLS_TERRITORY, NLS_CHARACTERSET的有效取值。
D) Locale-dependent SQL Function
SQL functions whose behavior depends on the values of NLS parameters are called locale-dependent.
依賴於NLS參數的SQL函數包括:
?TO_CHAR
?TO_DATE
?TO_NUMBER
?NLS_UPPER
?NLS_LOWER
?NLS_INITCAP
?NLSSORT
In all of the preceding functions, you can specify these NLS parameters:以上函數中可以指定的參數包括:
?NLS_DATE_FORMAT
?NLS_DATE_LANGUAGE
?NLS_NUMERIC_CHARACTERS
?NLS_CURRENCY
?NLS_ISO_CURRENCY
?NLS_DUAL_CURRENCY
?NLS_CALENDAR
?NLS_SORT
此外對於NLSSORT函數,還可以指定以下參數。
?NLS_LANGUAGE
?NLS_TERRITORY
按照'parameter=value' ['parameter=value']...的格式來指定參數。
例如,
select to_char(sysdate,'DD-MON-YYYY','NLS_DATE_LANGUAGE=''SIMPLIFIED CHINESE''') from dual;結果返回 "04-8月 -2011"
select * from employees order by nlssort(name,'NLS_SORT=SCHINESE_PINYIN_M');
E) NLS_LANG
NLS_LANG這個參數比較特殊,它是通過環境變數(UNIX)或者註冊表(WINDOWS)定義的。它分三部分,格式如下:
NLS_LANG = <Language>_<Territory>.<Charset>
<Language>部分設定了SESSION的NLS_LANGUAGE參數;
_<Territory>部分設定了SESSION的NLS_TERRITORY參數;
.<Charset>部分設定了CLIENT的字元集。它會影響到如何轉換用戶端的字元集到資料庫字元集。
這三部分可以只定義任意一個/幾個部分。
如果只有_<Territory>部分,<Language>預設等於AMERICAN;
如果只有<Language>部分,_<Territory>的值根據<Language>的值來決定;
如果既沒有_<Territory>部分也沒有<Language>部分,這2部分認等於AMERICAN_AMERICA;
如果沒有定義NLS_LANG,那麼預設值<Language>_<Territory>.US7ASCII,其中<Language>_<Territory>部分根據NLS_INSTANCE_PARAMETERS中的值來決定。
NLS_LANG參數不能通過ALTER SESSION或者ALTER SYSTEM來更改(但是可以更改NLS_LANGUAGE和NLS_TERRITORY),也不能定義在init.ora中。
資料庫建立的時候定義此參數也不影響資料庫的設定,包括不會影響資料庫的NLS_CHARACTERSET and NLS_NCHAR_CHARACTERSET 參數。
NLS_LANG參數的用戶端字元集定義部分<Charset>不能通過查詢任何系統資料表格或視圖得到。
(感覺實際上這個參數應該只會影響到用戶端,也就是NLS_SESSION_PARAMETERS)
NLS_LANG直接設定了NLS_LANGUAGE和NLS_TERRITORY參數,還間接設定了其他的NLS參數。
NLS_DATE_LANGUAGE 和 NLS_SORT 根據NLS_LANGUAGE決定取值;
NLS_CURRENCY, NLS_DUAL_CURRENCY, NLS_ISO_CURRENCY, NLS_DATE_FORMAT, NLS_TIMESTAMP_FORMAT, NLS_TIMESTAMP_TZ_FORMAT, NLS_NUMERIC_CHARACTERS 根據NLS_TERRITORY決定取值。
注1:NLS_LANG在WINDOWS註冊表中的位置:HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_xxx
There are 3 levels at which you can set NLS parameters: Database, Instance and Session.
If a parameter is defined at more than one level then the rules on which one takes precedence are quite straightforward:
1. NLS database settings are superseded by NLS instance settings
2. NLS database & NLS instance settings are superseded by NLS session settings
Applications can check the session, instance, and database NLS parameters by querying the following data dictionary views:
A) NLS_SESSION_PARAMETERS
NLS_SESSION_PARAMETERS shows the NLS parameters and their values for the session that is querying the view.
It does not show information about the character set.
The values of NLS parameters set by "ALTER SESSION ". If there is no explicit "ALTER SESSION " statement done then it reflects the setting of the corresponding NLS parameter on the client derived from the NLS_LANG variable.
The NLS_LANG parameter has three components: language, territory, and character set. Specify it in the following format, including the punctuation:
NLS_LANG = <Language>_<Territory>.<Charset>
If NLS_LANG is specified with only the <Territory> part then AMERICAN is used as default <Language>.
If NLS_LANG is specified with only the <Language> part then the <Territory> defaults to a setting based on <Language>.
If NLS_LANG is specified without the <Language>_<Territory> part then the <Language>_<Territory> part defaults to AMERICAN_AMERICA.
If the NLS_LANG is not set at all, then it defaults to <Language>_<Territory>.US7ASCII and the values for the <Language>_<Territory> part used are the ones found in
NLS_INSTANCE_PARAMETERS.
The language and territory components of the NLS_LANG parameter determine the default values for other detailed NLS parameters, such as date format, numeric characters, and linguistic sorting.
If NLS_DATE_LANGUAGE or NLS_SORT are not set then they are derived from NLS_LANGUAGE.
If NLS_CURRENCY, NLS_DUAL_CURRENCY, NLS_ISO_CURRENCY, NLS_DATE_FORMAT, NLS_TIMESTAMP_FORMAT, NLS_TIMESTAMP_TZ_FORMAT, NLS_NUMERIC_CHARACTERS are not set then they are derived from NLS_TERRITORY
* NLS_LANG cannot be changed by ALTER SESSION, NLS_LANGUAGE and NLS_TERRITORY can.
* The <clients characterset> part of NLS_LANG is NOT shown in any system table or view.
* On Windows you have two possible options, normally the NLS_LANG is set in the registry, but it can also be set in the environment, however this is not often done and generally not recommended to do so. The value in the environment takes precedence over the value in the registry and is used for ALL Oracle_Homes on the server if defined as a system environment variable.
* Oracle strongly recommends that you set the NLS_LANG on the client at least to NLS_LANG=.<clients characterset>
B) NLS_INSTANCE_PARAMETERS
NLS_INSTANCE_PARAMETERS shows the current NLS instance parameters that have been explicitly set and the values of the NLS instance parameters.
These are the settings in the init.ora of the database at the moment that the database was started or set through ALTER SYSTEM.
If the parameter is not explicitly set in the init.ora or defined by ALTER SYSTEM then its value is NOT derived from a "higher" parameter (we are talking about parameters like NLS_SORT that derive a default from NLS_LANGUAGE in NLS_SESSION_PARAMETERS, this is NOT the case for NLS_INSTANCE_PARAMETERS)
* NLS_LANG is not an init.ora parameter; NLS_LANGUAGE and NLS_TERRITORY are so you need to set NLS_LANGUAGE and NLS_TERRITORY separately.
* You cannot define the <clients characterset> or NLS_LANG in the init.ora.The client characterset is defined by the NLS_LANG on the client OS (see above).
* You cannot define the database characterset in the init.ora. The database characterset is defined by the "Create Database" command.
* These settings take precedence above the NLS_DATABASE_PARAMETERS.
* These values are used for the NLS_SESSION_PARAMETERS if the client the NLS_LANG is NOT set.
* The NLS_LANGUAGE in the instance parameters also declares the language for the server error messages in alert.log and in trace files.
C) NLS_DATABASE_PARAMETERS
NLS_DATABASE_PARAMETERS shows the values of the NLS parameters for the database. The values are stored in the database.
Defaults to AMERICAN_AMERICA if there are no parameters explicitly set in the init.ora during database creation time. If there is parameters set in the init.ora during database creation you see them here. There is no way to change these after the database creation. Do NOT attempt to update system tables to bypass these settings! These settings are used to give the database a default if the INSTANCE and SESSION parameters are not set.
The database (national) character set NLS_(NCHAR)_CHARACTERSET) is defined by the "Create Database" command.
* The NLS_CHARACTERSET and NLS_NCHAR_CHARACTERSET parameters cannot be overridden by instance or session parameters.
They are defined by the value specified in the "CREATE DATABASE command and are not intended to be changed afterwards dynamically. Do NOT update system tables to change the character set. This can corrupt your database and potentially make it impossible to open the database again.
* Setting the NLS_LANG during the creation of the database does not influence the NLS_DATABASE_PARAMETERS.
* The NLS_LANG set during the database creation has NO impact on the database National Characterset.