默认情况下, Oracle客户端程序的字符集都由环境变量(NLS_LANG)决定, 很多时侯会因为忽略了环境变量的检查, 尤其是在Shell或Perl脚本中调用时, 导致导出的数据变成了乱码. 在OCI 8的接口中, 可以使用OCIEnvNlsCreate函数在程序中指定客户端字符集, 从而跳过环境变量的影响, 于是就在SQLULDR2工具中新增了两个选项, 用于指定导出时了字符集.

    新的SQLULDR2工具命令行帮助如下所示:

SQL*UnLoader: Fast Oracle Text Unloader (GZIP, Parallel), Release 4.0.1
(@) Copyright Lou Fangxin (AnySQL.net) 2004 – 2010, all rights reserved.

Usage: SQLULDR2 keyword=value [,keyword=value,...]

Valid Keywords:
  user    = username/password@tnsname
  sql    = SQL file name
  query  = select statement
  field  = separator string between fields
  record  = separator string between records
  rows    = print progress for every given rows (default, 1000000)
  file    = output file name(default: uldrdata.txt)
  log    = log file name, prefix with + to append mode
  fast    = auto tuning the session level parameters(YES)
  text    = output type (MYSQL, CSV, MYSQLINS, ORACLEINS, FORM, SEARCH).
  charset = character set name of the target database.
  ncharset= national character set name of the target database.
  parfile = read command option from parameter file

  for field and record, you can use ’0x’ to specify hex character code,
  \r=0x0d \n=0x0a |=0x7c ,=0x2c, \t=0×09, :=0x3a, #=0×23, “=0×22 ‘=0×27

    在一个GBK的数据库中存入”中国”两个字, 然后以GBK和UTF8两种不同的字符集进行导出, 在命令行指定字符集设置.

C:\>sqluldr2 scott/tiger query=test charset=ZHS16GBK
      0 rows exported at 2010-06-24 09:12:27, size 0 MB.
      1 rows exported at 2010-06-24 09:12:27, size 0 MB.
        output file uldrdata.1.txt closed at 1 rows, size 0 MB.

C:\>dir uldrdata.1.txt
2010-06-24  09:12                5 uldrdata.1.txt

C:\>sqluldr2 scott/tiger query=test  charset=UTF8
      0 rows exported at 2010-06-24 09:12:42, size 0 MB.
      1 rows exported at 2010-06-24 09:12:42, size 0 MB.
        output file uldrdata.1.txt closed at 1 rows, size 0 MB.

C:\>dir uldrdata.1.txt
2010-06-24  09:12                7 uldrdata.1.txt

    用GBK导出时, 文件大小为5个字节, 两个汉字(一个汉字占两个字节)再加一个换行符. 而用UTF8导出时, 文件大小为7, 两个汉字(一个汉字占三个字节)加上一个换行符. 如果用SQLULDR2为特定的数据库写脚本程序时, 在命令行指定字符集, 将使脚本更加强壮.

    另外, SQLULDR2将终结新功能的开发了, 除非有商业的需求.