Oracle Unicode假脱机

问题描述:

如何将数据从表假脱机到包含Unicode字符的文件中?

How can I spool data from a table to a file which contains Unicode characters?

我有一个从SQL * Plus屏幕执行的sql文件,其内容为:

I have a sql file which I execute from SQL*Plus screen and its content is:

SET ECHO OFF
SET FEEDBACK OFF
SET HEADING OFF
SET PAGESIZE 0
SPOOL STREET_POINT_THR.BQSV
SELECT GEOX||'`'||GEOY||'`'||UNICODE_DESC||'`'||ASCII_DESC 
FROM GEO.STREET_POINTS;
SPOOL OFF

使用正确的设置,您的脚本即可与SQL * Plus一起使用.这是我所做的测试:

with the right settings your script does work with SQL*Plus. Here is what I did to test it:

  • (显然)您的数据库必须支持unicode.如有必要,请使用NVARCHAR2.
  • 正确设置您的客户端应用程序.确保您的NLS_LANG变量设置正确,它必须支持unicode.我将我的设置为AMERICAN_ENGLISH.UTF8.尽管SQL * Plus的DOS窗口不会显示所有unicode字符,但它们将被正确地后台处理到文件中.
  • (显然也是)确保读取假脱机文件的应用程序以正确的字符集打开它.
  • (obviously) your database must support unicode. Use NVARCHAR2 if necessary.
  • Setup your client application correctly. make sure your NLS_LANG variable is set correctly, it must support unicode. I set mine to AMERICAN_ENGLISH.UTF8. While the DOS window of SQL*Plus won't display all unicode characters, they will be spooled correctly into the file.
  • (obviously too) make sure the application that reads the spooled file opens it in the right character set.

现在是脚本:

SQL> select * from v$nls_parameters where parameter = 'NLS_CHARACTERSET';

PARAMETER          VALUE
------------------ ------
NLS_CHARACTERSET   UTF8

SQL> create table street_points (data varchar2(10));

Table created

SQL> INSERT INTO street_points VALUES (chr(53401)||chr(53398));

1 row inserted

这将插入俄语字符ЙЖ

SQL> SPOOL STREET_POINT_THR.BQSV
SQL> SELECT * FROM STREET_POINTS;
ðÖðû
SQL> SPOOL OFF

使用带有正确字符集(UTF-8)的文本编辑器(在我的情况下为jEdit)打开的文件可以正确显示字符.

The file, opened with a text editor (jEdit in my case) with the correct character set (UTF-8) displays the characters correctly.