Oracle   发布时间:2022-05-17  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Setting NLS_LANG Value for Oracle大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Introduction

@H_477_6@many times,when you have an Oracle application and you have to support special characters like ö,ä,ü,é,è or currency symbols (e.g. ),you encounter problems with proper display. Mostly,this problem is caused by improper setTing of NLS_LANG value.

NLS_LANG sets the language and territory used by the clIEnt application and the database server. It also sets the clIEnt‘s character set,which is the character set for data entered or displayed by a clIEnt program.

Character Set of Database

When an Oracle Database is created,the DBA has to specify the CHaraCTER SET and thNATIONAL CHaraCTER SET.

Nowadays,the default values are:

  • AL32UTF8 for CHaraCTER SET and
  • AL16UTF16 for NATIONAL CHaraCTER SET

these Database character sets define which characters (in which format) can be stored in CHARCLOBVARCHAR2resp. in NCHARNCLOBnvarchar2 column. On an exisTing database,you can query the values with:

SELECT * 
FROM NLS_DATABASE_ParaMETERS WHERE ParaMETER liKE %CHaraCTERSET‘; ParaMETER VALUE ========================================== NLS_CHARACTERSET AL32UTF8 NLS_NCHAR_CHaraCTERSET AL16UTF16 2 row(s) SELEcted.

The database character sets do not define if and how charaters are displayed in your clIEnt application!

Some Facts of NLS_LANG

Format of NLS_LANG deFinition is NLS_LANG = LANGUAGE_TERRITORY.CHARSET

All components of the NLS_LANG deFinition are optional; any item that is not specifIEd uses its default value. If you specify territory or character set,then you must include the preceding delimiter [underscore (_) for territory,period (.) for character set]. Otherwise,the value is parsed as a language name.

Following deFinitions are all valID:

  • NLS_LANG=.WE8ISO8859P1
  • NLS_LANG=_GERMANY
  • NLS_LANG=AMERICAN
  • NLS_LANG=ITAliAN_.WE8MSWIN1252
  • NLS_LANG=_BElgiUm.US7ASCII

If NLS_LANG value is not provIDed,then Oracle defaults it to AMERICAN_AMERICA.US7ASCII.

LANGUAGE and TERRITORY set the default value for many other NLS Parameters,see this table to get an overvIEw. CHARSET is used to let Oracle kNow what character set you are using on the clIEnt sIDe,so Oracle can do the proper conversion. SetTing the LANGUAGE and TERRITORY parameters of NLS_LANG has nothing to do with the ability to store characters in a database. Here,you see a List of available Languages, Territoriesand Character Sets.

You can change the language and territory of your session by:

ALTER SESSION SET NLS_LANGUAGE = ...‘; respective ALTER SESSION SET NLS_TERRITORY = ...‘;

However,you cAnnot change your clIEnt charset with any sql command,it is set only by the NLS_LANG value.

Some setTing can be explicitly set in sql functions,for example:

SELECT TO_CHAR(sysdate,DD Month‘,NLS_DATE_LANGUAGE = french‘) FROM dual;

other can not,e.g.:

SELECT TRUNC(SYSDATE,DY‘,NLS_TERRITORY = AMERICA‘) AS FirsT_DAY_OF_WEEK FROM dual;

does not work.

You cAnnot query your clIEnt charset by any Dictionary or dynamic perfoRMANce vIEw or any other sql command. Also,Dictionary vIEw NLS_SESSION_ParaMETERS shows the database character set,not the clIEntcharacter set!

You can run query:

SELECT CLIENT_CHARSET 
FROM v$sesSION_CONNECT_INFO;

However,the values appear not reliable. Sometimes,it shows NulL or "unkNown".

DeFinition of NLS_LANG

NLS_LANG can be set by Environment variable (e.g. SET NLS_LANG=AMERICAN_AMERICA.WE8MSWIN1252) or by your Registry at HKEY_LOCAL_MACHINE\Software\Oracle\KEY_{OracLE_HOME_namE}\NLS_LANG,resp. HKEY_LOCAL_MACHINE\Software\Wow6432Node\Oracle\KEY_{OracLE_HOME_namE}\NLS_LANG for 32-bit Oracle ClIEnt on a 64-bit windows. The Environment variable takes precedence over Registry entry.

You can interrogate exisTing values with:

windows:

reg query HKEY_LOCAL_MACHINE\Software\Oracle\KEY_{OracLE_HOME_namE} /f NLS_LANG
reg query HKEY_LOCAL_MACHINE\Software\Wow6432Node\Oracle\KEY_{OracLE_HOME_namE} /f NLS_LANG
set NLS_LANG

Unix/linux:

echo $NLS_LANG

Proper Value of NLS_LANG

Usually,the values for LANGUAGE and TERRITORY are obvIoUs and less critical in the application. The most interesTing is the CHaraCTER SET value. Many times,you read in forums (and sometimes even in official documentation): "The clIEnt NLS_LANG character set must be the same value as the database character set" - This is simply not true! ConsIDer the database has two character sets,the "normal" and the national character set. On the clIEnt sIDe,you have only one value,so actually they cAnnot be equal. Some character sets are available only on Client side which also vinDicates my statement.

There are two requirements for the NLS_LANG character set:

  1. The NLS_LANG character set must support the characters you like to use in your application.
  2. The NLS_LANG character set must match the character set (or enCoding) of your application.
  3. @H_338_301@

    Some applications/drivers load NLS_LANG deFinition when at launch and derive their character set from NLS_LANG value. In such case,it becomes easIEr and only the first requirement applIEs.

    NLS_LANG with sql*Plus

    sql*Plus inherits the character set from the terminal session where you started it. On windows,you get the current character set (here called "codepage") with chcp,the linux/Unix equivalent is locale charmap or echo $LANG. Thus,a proper setTing would be for example:

    C:\>chcp
    Active codepage: 850.
    
    C:\>set NLS_LANG=.WE8PC850
    
    C:\>sqlplus ...

    With chcp,you can also change your codepage,e.g., chcp 1252. You can use the small batch file to change the codepage of your command line window peRMANently:

    @H_944_345@@ECHO off
    
    SET ROOT_KEY="HKEY_CURRENT_USER" FOR /f "skip=2 tokens=3" %%i in _ (reg query HKEY_LOCAL_MACHINE\SYstem\CurrentControlSet\Control\Nls\codepage /v OEMCP‘) do set OEMCP=%%i ECHO. ECHO ............................................... ECHO SELEct codepage ECHO ............................................... ECHO. ECHO 1 - CP1252 ECHO 2 - UTF-8 ECHO 3 - CP850 ECHO 4 - ISO-8859-1 ECHO 5 - iso-8859-15 ECHO 6 - US-ASCII ECHO. ECHO 9 - reset to System Default (CP%OEMCP%) ECHO 0 - EXIT ECHO. SET /P CP="SELEct a codepage: " if %CP%==1 ( echo Set default codepage to CP1252 reg add "%rOOT_KEY%\Software\Microsoft\Command Processor" /v autorun /t REG_SZ /d "chcp 1252" /f ) else if %CP%==2 ( echo Set default codepage to UTF-8 reg add "%rOOT_KEY%\Software\Microsoft\Command Processor" /v autorun /t REG_SZ /d "chcp 65001" /f ) else if %CP%==3 ( echo Set default codepage to CP850 reg add "%rOOT_KEY%\Software\Microsoft\Command Processor" /v autorun /t REG_SZ /d "chcp 850" /f ) else if %CP%==4 ( echo Set default codepage to ISO-8859-1 add "%rOOT_KEY%\Software\Microsoft\Command Processor" /v autorun /t REG_SZ /d "chcp 28591" /f ) else if %CP%==5 ( echo Set default codepage to iso-8859-15 add "%rOOT_KEY%\Software\Microsoft\Command Processor" /v autorun /t REG_SZ /d "chcp 28605" /f ) else if %CP%==5 ( echo Set default codepage to ASCII add "%rOOT_KEY%\Software\Microsoft\Command Processor" /v autorun /t REG_SZ /d "chcp 20127" /f ) else if %CP%==9 ( echo reset codepage to System Default reg delete "%rOOT_KEY%\Software\Microsoft\Command Processor" /v autoRun /f ) else if %CP%==0 ( echo Bye ) else ( echo InvalID choice pause )

    Note,the setTings will apply only for the current user. If you like to set it for all users,replace line:

    SET ROOT_KEY="HKEY_CURRENT_USER"

    by:

    SET ROOT_KEY="HKEY_LOCAL_MACHINE"

    Be careful with codepage UTF-8 (chcp 65001) there is a BUG,see this discussion. I do not kNow whether this has been fixed in more recent windows / sql*Plus versions.

    NLS_LANG with .sql files

    When you run sql files in sql*Plus,check the save options of your editor. Typically,you can choose values like ISO-8859-1UTF-8ANSICP1252 as enCoding. Term "ANSI" denotes the default windows code pages. On a Western PC,this is CP1252.

    You can interrogate default windows code pages with:

    @H_818_618@ HIDe   copy Code
C:\>reg query HKEY_LOCAL_MACHINE\SYstem\CurrentControlSet\Control\Nls\codepage /v ACP

HKEY_LOCAL_MACHINE\SYstem\CurrentControlSet\Control\Nls\codepage
    ACP    REG_SZ    1252

C:\>

or read "ANSI codepage" from this table National Language Support (NLS) API Reference for any locale.

You must set character set of NLS_LANG according to the enCoding of your text editor. Here is a List of available Code Pages.

NLS_LANG in Your .NET Application

  • ODP.NET Managed Driver is not NLS_LANG sensitive.
    it is only .NET locale sensitive. (See Data Provider for .NET Developer‘s GuidE)
  • ODBC,ODP.NET and olE DB provIDers from Oracle read NLS_LANG value when they are loaded and inherit the deFinition,resp. ensures proper character conversion for any clIEnt/database character setTing.
  • ODBC,ADO.NET and olE DB provIDers from Microsoft also read NLS_LANG value when they are loaded. However,they have some limitations,especially in terms of Unicode.

How to Determine the Character Set of My Application If Not KNown?

First of all,you should consult the documentation of your application and used drivers.

I developed the following approach if you still have no clue about the used character set.

  • Set your NLS_LANG to NLS_LANG=.AL32UTF8
  • Connect with sql*Plus to a database with UTF-8 support,i.e.,character set AL32UTF8
    When the clIEnt character set is equal to the database character set,then no character conversion takes place and all bytes are transferred "as they are"
  • In your application,run a query with special character like this:
SELEct dump(€‘) from dual; DUMP(€‘) ----------------- Typ=96 Len=1: 164

Then you can estimate the character set with a function written in C# like this:

byte[] o = new byte[] { 164 }; foreach ( var enc in EnCoding.GetEnCodings() ) { var convertedString = enc.GetEnCoding().GetBytes("€"); if ( convertedString.SequenceEqual(o) ) Console.Writeline(String.Format("{0}\t{1}\t{2}",enc.codepage,enc.name,enc.displayName)); } 

The function will print a List of potential character sets used by your application. Sometimes,the printout gives you obvIoUsly used character set,sometimes you have to use more other special characters. Some codepages differ only in a single character!

what To Do If My Characters Are Still Not Properly displayed?

  • check carefully the documentation of your application and used drivers. Perhaps they are old and do not support Unicode yet. Make an update to the latest version of drivers.
  • check if your Font supports desired characters. You can use for example this page Font Support for Unicode Characters to verify used Fonts.
  • check the real content of your database. Run query like SELECT DUMP(THE_columN,1016) FROM ... to see the bytes in the table. Perhaps the data have been inserted by a clIEnt with wrong NLS_LANGdeFinition. Don‘t be scared,usually you have to investigate only a few characters/bytes to get a result.    

文献

https://docs.Oracle.com/database/121/NLSPG/applocaledata.htm#GUID-9529D1B5-7366-4195-94B5-0F90F3B472E1

https://i.cnblogs.com/EditPosts.aspx?opt=1

https://docs.Oracle.com/html/B10131_02/gblsupp.htm

https://docs.Oracle.com/cd/E12102_01/books/AnyInstAdm784/AnyInstAdmPreInstall18.html

https://www.unicode.org/wg2/iso10646/edition5/charts/iso10646-5th-CodeCharts.pdf

https://www.ibm.com/support/kNowledgecenter/en/SS6Qym_9.2.0/com.ibm.Help.install.doc/t_ConfiguringTheNLS_LANGParameterForAnOracleClIEnt.HTML

转自:

https://www.codeproject.com/Tips/1068282/SetTing-NLS-LANG-Value-for-Oracle

大佬总结

以上是大佬教程为你收集整理的Setting NLS_LANG Value for Oracle全部内容,希望文章能够帮你解决Setting NLS_LANG Value for Oracle所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: