nls_lang相关的问题

nls_lang是个什么东西?

NLS (National Language Support) parameters determine the locale-specific behavior on both the client and the server.

NLS_LANG的组成: NLS_LANG=_.
为什么是客户端的字符集? 
文档 1577370.1      
几个注意的要点:
    
       NLS_LANG定义的字符集不更改您客户端的字符集,NLS_LANG只是让Oracle知道您客户端使用的字符集,所以Oracle可以在NLS_CHARACTERSET和客户端编码间转化。

        这句话什么意思?oracle需要知道客户端的字符集是什么,好在nls_characterset和客户端编码(就是nls_lang最后一部分指定的)之间做转换。那么,nls_lang是只存在于服务器端?不是的,在server端和client端都有这个参数,在服务器端,可以在表中查出来,这是在安装时设置的字符集,在客户端,不同的操作系统有不同的设置。
nls_database_parametersnls_database_parameters         
1.在windows中的设置(实际上这是client端的设置)
         
文档 ID 1577370.1

             注册表中:版本8和9i中,位置在HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOMEx,10以上版本,位置在
   
          
            KEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_

           环境变量中:在环境变量中设置NLS_LANG属性
           命令行中:SET设置
    

           2.在linux中的设置:(实际上这是client端的设置)
 
            
           文档 1548858.1
          local的设置,如果需要修改,请更正

          $local

         建议的设置是utf-8,设置方法是:
         $ export LC_ALL=en_US.UTF-8

         3.导出导入exp\imp涉及到的字符集转换
       

        NLS considerations in Import/Export - Frequently Asked Questions (文档 ID 227332.1)
      

       How does NLS affect exp/imp (old import/export )?

        Imp and exp are client products,, in the same way as SQL*Plus or Oracle Forms and will therefore translate characters from the database character set to that defined by NLS_LANG. The character set used for the export will be stored in the export file and, when the file is imported, the import will check the character set that was used. If it is different to that defined by NLS_LANG at the import site, the characters will be  translated to the import character set and then, if necessary, to the database character set.
       这句话说的很好,imp和exp是客户端工具,那么就涉及一个从server导出数据到客户端的字符集转换问题了,而转换的依据就是这个NLS_LANG参数,这个字符集,是要存储在导出文件中的。导入时,会检查这个字符集,如果在导出文件的字符集与要导入的数据库定义的NLS_LANG定义的不一样,那么,字符集转换就发生了。
       简单的说:导出时,检查database server的字符集和客户端字符集,如果不同,发生转换。这个字符集信息存入导出文件。在导入时,也检查数据库字符集和导出文件的字符集是否一致。
      数据库的字符集:
      select value from nls_database_parameters where parameter='NLS_CHARACTERSET';
   on unix this is:
       $ set NLS_LANG=AMERICAN_AMERICA.WE8MSWIN1252
       $ export NLS_LANG
       $ exp ....

       on windows this is:

       c:\>set NLS_LANG=AMERICAN_AMERICA.WE8MSWIN1252
       c:\>exp ....
       客户端的字符集怎么看?就是上面分开写的在windows和linux上设置nls_lang的部分。

      案例1:server字符集和客户端字符集一致(在windows上测试)
      检查数据库字符集:(表示服务器端字符集)

      SQL> select value from nls_database_parameters where parameter='NLS_CHARACTERSET';

     VALUE
     --------------------------------------------------------------------------------
    ZHS16GBK
检查注册表字符集:(表示客户端字符集)
     
NLS_LANG的值是SIMPLIFIED CHINESE_CHINA.ZHS16GBK  
在环境变量和命令行中,都没有设置nls_lang

现在,这两个字符集是匹配的,exp试试看:

C:\Users\Administrator>exp test/test@mytest file=d:\exptest.dmp owner=test

Export: Release 11.2.0.1.0 - Production on 星期六 6月 7 10:50:48 2014

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning and Real Application Testing options
已导出 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集
导出没有提示字符集转换。
然后drop掉一个表,导入看看。

C:\Users\Administrator>imp test/test@mytest file=d:\exptest.dmp fromuser=test touser=test tables=TEST

Import: Release 11.2.0.1.0 - Production on 星期六 6月 7 10:52:56 2014

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning and Real Application Testing options

经由常规路径由 EXPORT:V11.02.00 创建的导出文件
已经完成 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集中的导入
. 正在将 TEST 的对象导入到 TEST
. . 正在导入表                          "TEST"导入了           7 行
成功终止导入, 没有出现警告。

也没有转换,检查TEST表,汉字没有乱码

案例2:手工修改掉客户端字符集(在windows上测试)

 
regedit修改掉nls_lang为AMERICAN_AMERICA.US7ASCII

 

 database server字符集没动

SQL> select value from nls_database_parameters where parameter='NLS_CHARACTERSET';

     VALUE
     --------------------------------------------------------------------------------
    ZHS16GBK

    现在就有server和client字符集不一致的问题了。
   导出试试看:

 

C:\Users\Administrator>exp test/test@mytest file=d:\exptest.dmp owner=test

Export: Release 11.2.0.1.0 - Production on Sat Jun 7 10:59:24 2014

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Produc
With the Partitioning and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses ZHS16GBK character set (possible charset conversion)

注意:1.这里有字符集转换提示。
           2.exp的提示,都变成了英文了。这说明命令行受到注册表里nls_lang的影响。
导入:

C:\Users\Administrator>imp test/test@mytest file=d:\exptest.dmp fromuser=test touser=test tables=TEST

Import: Release 11.2.0.1.0 - Production on Sat Jun 7 11:01:54 2014

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning and Real Application Testing options

Export file created by EXPORT:V11.02.00 via conventional path
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses ZHS16GBK character set (possible charset conversion)
. importing TEST's objects into TEST
. . importing table                         "TEST"          7 rows imported
有字符集转换提示。

检查TEST表,发现没有乱码,正常。说明,服务端是 ZHS16GBK客户端是US7ASCII,不会发生乱码。

        结论:导出导入涉及到server到client的字符集问题,exp\imp都是客户端工具,如果这两端字符集匹配,没转换发生,如果不匹配,有转换发生,但是不一定会产生乱码。客户端的字符集的设置nls_lang,在windows上,有三个位置,注册表、环境变量、命令行,在linux上,是lc_all,就是local的设置。

 

  

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/263455/viewspace-1176874/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/263455/viewspace-1176874/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值