Writes the current query results to a file.
OUTPUT TO filename [ APPEND ] [ VERBOSE ] [ FORMAT output-format ] [ ESCAPE CHARACTER character ] [ DELIMITED BY string ] [ QUOTE string [ ALL ] ] [ COLUMN WIDTHS ( integer, … ) ] [ HEXADECIMAL { ON | OFF | ASIS } ] [ ENCODING encoding ] [ WITH COLUMN NAMES ]
SELECT * FROM Employees; OUTPUT TO employees.txt FORMAT TEXT
SELECT * FROM Employees; OUTPUT TO employees.txt APPEND VERBOSE
Execute this statement with HEXADECIMAL ON:
SELECT 'line1\x0aline2'; OUTPUT TO file.txt HEXADECIMAL ON
The result is a file with one line in it, containing this text:
line10x0aline2
Execute the same statement with HEXADECIMAL OFF:
line1\x0aline2
If you set HEXADECIMAL to ASIS, you get a file with two lines:
'line1 line2'
Using ASIS generates two lines, because the embedded line feed character has been exported without being converted to a two-digit hex representation, and without a prefix.
The OUTPUT statement copies the information retrieved by the current query to a file.
You can specify the output format with the optional FORMAT clause. If no FORMAT clause is specified, the Interactive SQL OUTPUT_FORMAT option setting is used.
The current query is the SELECT or LOAD TABLE statement that generated the information that appears on the Results tab in the Results pane. The OUTPUT statement reports an error if there is no current query.
APPEND—This optional keyword is used to append the results of the query to the end of an existing output file without overwriting the previous contents of the file. If the APPEND clause is not used, the OUTPUT statement overwrites the contents of the output file by default. The APPEND keyword is valid if the output format is TEXT, FIXED, or SQL.
VERBOSE—When the optional VERBOSE keyword is included, error messages about the query, the SQL statement used to select the data, and the data itself are written to the output file. If VERBOSE is omitted (the default), only the data is written to the file. The VERBOSE keyword is valid if the output format is TEXT, FIXED, or SQL.
FORMAT—Allowable output formats are:
TEXT—The output is a TEXT format file with one row per line in the file. All values are separated by commas, and strings are enclosed in apostrophes (single quotes). The delimiter and quote strings can be changed using the DELIMITED BY and QUOTE clauses. If ALL is specified in the QUOTE clause, all values (not just strings) are quoted. TEXT is the default output format.
Three other special sequences are also used. The two characters \n represent a newline character, \\ represents a single \, and the sequence \xDD represents the character with hexadecimal code DD.
If you are exporting Java methods that have string return values, you must use the HEXADECIMAL OFF clause.
FIXED—The output is fixed format with each column having a fixed width. The width for each column can be specified using the COLUMN WIDTHS clause. No column headings are output in this format.
If COLUMN WIDTHS is omitted, the width for each column is computed from the data type for the column, and is large enough to hold any value of that data type. The exception is that LONG VARCHAR and LONG BINARY data defaults to 32KB.
HTML—The output is in the Hyper Text Markup Language format.
SQL—The output is an Interactive SQL INPUT statement required to recreate the information in the table.
XML—The output is an XML file encoded in UTF-8 and containing an embedded DTD. Binary values are encoded in CDATA blocks with the binary data rendered as 2-hex-digit strings. The LOAD TABLE statement does not accept XML as a file format.
ESCAPE CHARACTER—The default escape character for characters stored as hexadecimal codes and symbols is a backslash (\), so \x0A is the line feed character, for example.
This default can be changed using the ESCAPE CHARACTER clause. For example, to use the exclamation mark as the escape character, enter:
... ESCAPE CHARACTER '!'
DELIMITED BY—The DELIMITED BY clause is for the TEXT output format only. The delimiter string is placed between columns (default comma).
QUOTE—The QUOTE clause is for the TEXT output format only. The quote string is placed around string values. The default is a single quote character. If ALL is specified in the QUOTE clause, the quote string is placed around all values, not just around strings.
COLUMN WIDTHS—The COLUMN WIDTHS clause is used to specify the column widths for the FIXED format output.
HEXADECIMAL—The HEXADECIMAL clause specifies how binary data is to be unloaded for the TEXT format only. When set to ON, binary data is unloaded in the format 0xabcd. When set to OFF, binary data is escaped when unloaded (\xab\xcd). When set to ASIS, values are written as is, that is, without any escaping—even if the value contains control characters. ASIS is useful for text that contains formatting characters such as tabs or carriage returns.
ENCODING—Specifies the encoding that is used to write the file. The ENCODING clause can be used only with the TEXT format.
If encoding is not specified, Interactive SQL determines the code page that is used to write the file as follows, where code page values occurring earlier in the list take precedence over those occurring later:
The code page specified with the DEFAULT_ISQL_ENCODING option (if this option is set)
The default code page for the computer Interactive SQL is running on
SQL—Vendor extension to ISO/ANSI SQL grammar.
Sybase—Not applicable.
None