Use this statement to output the current query results to a file.
OUTPUT TO file-name
[ APPEND ]
[ VERBOSE ]
[ FORMAT output-format ]
[ ESCAPE CHARACTER character ]
[ ESCAPES { ON | OFF }
[ DELIMITED BY string ]
[ QUOTE string [ ALL ] ]
[ COLUMN WIDTHS (integer, ...) ]
[ HEXADECIMAL { ON | OFF | ASIS } ]
[ ENCODING encoding ]
output-format :
ASCII | DBASEII | DBASEIII | EXCEL
| FIXED | FOXPRO | HTML | LOTUS
| SQL | XML
encoding : string or identifier
APPEND clause 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 ASCII, FIXED, or SQL.
VERBOSE clause 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. Lines that do not contain data are prefixed by two hyphens. If VERBOSE is omitted (the default) only the data is written to the file. The VERBOSE keyword is valid if the output format is ASCII, FIXED, or SQL.
FORMAT clause Allowable output formats are:
ASCII The output is an ASCII 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.
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. This is the default output format.
DBASEII The output is a dBASE II format file which includes column definitions. Note that a maximum of 32 columns can be output. Column names are truncated to 11 characters, and each row of data in each column is truncated to 255 characters.
DBASEIII The output is a dBASE III format file which includes column definitions. Note that a maximum of 128 columns can be output. Column names are truncated to 11 characters, and each row of data in each column is truncated to 255 characters.
EXCEL The output is an Excel 2.1 worksheet. The first row of the worksheet contains column labels (or names if there are no labels defined). Subsequent worksheet rows contain the actual table data.
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 the COLUMN WIDTHS clause 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 default to 32 KB.
FOXPRO The output is a FoxPro format file which includes column definitions. Note that a maximum of 128 columns can be output. Column names are truncated to 11 characters. Column names are truncated to 11 characters, and each row of data in each column is truncated to 255 characters.
HTML The output is in the Hyper Text Markup Language format.
LOTUS The output is a Lotus WKS format worksheet. Column names are put as the first row in the worksheet. Note that there are certain restrictions on the maximum size of Lotus WKS format worksheets that other software (such as Lotus 1-2-3) can load. There is no limit to the size of file Interactive SQL can produce.
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 INPUT statement does not accept XML as a file format.
ESCAPE CHARACTER clause The default escape character for characters stored as hexadecimal codes and symbols is a backslash (\), so \x0A is the linefeed character, for example.
This can be changed using the ESCAPE CHARACTER clause. For example, to use the exclamation mark as the escape character, you would enter
... ESCAPE CHARACTER '!'
New line characters can be included as the combination \n, other characters can be included in data as hexadecimal ASCII codes, such as \x09 for the tab character. A sequence of two backslash characters ( \\ ) is interpreted as a single backslash. A backslash followed by any character other than n, x, X, or \ is interpreted as two separate characters. For example, \q inserts a backslash and the letter q.
ESCAPES clause With ESCAPES turned on (the default), characters following the backslash character are recognized and interpreted as special characters by the database server. With ESCAPES turned off, the characters are written exactly as they appear in the source.
DELIMITED BY clause The DELIMITED BY clause is for the ASCII output format only. The delimiter string is placed between columns (default comma).
QUOTE clause The QUOTE clause is for the ASCII 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 clause The COLUMN WIDTHS clause is used to specify the column widths for the FIXED format output.
HEXADECIMAL clause The HEXADECIMAL clause specifies how binary data is to be unloaded for the ASCII 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 clause The encoding argument allows you to specify the encoding that is used to write the file. The ENCODING clause can only be used with the ASCII format.
For more information on how to obtain the list of SQL Anywhere supported encodings, see Supported character sets.
With Interactive SQL, if encoding is not specified, the encoding that is used to write the file is determined as follows, where encoding values occurring earlier in the list take precedence over those occurring later in the list:
the encoding specified with the default_isql_encoding option (if this option is set)
the encoding specified with the -codepage option when Interactive SQL was started
the default encoding for the computer Interactive SQL is running on
For more information about Interactive SQL and encodings, see default_isql_encoding option [Interactive SQL].
The OUTPUT statement copies the information retrieved by the current query to a file.
The output format can be specified with the optional FORMAT clause. If no FORMAT clause is specified, the Interactive SQL output_format option setting is used (see output_format option [Interactive SQL]).
The current query is the 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.
Because the INPUT statement is an Interactive SQL command, it cannot be used in any compound statement (such as IF) or in a stored procedure. See Statements allowed in procedures, triggers, events, and batches.
None.
In Interactive SQL, the Results tab displays only the results of the current query. All previous query results are replaced with the current query results.
SQL/2003 Vendor extension.
Place the contents of the Employees table in a file in ASCII format:
SELECT * FROM Employees; OUTPUT TO Employees.txt FORMAT ASCII;
Place the contents of the Employees table at the end of an existing file, and include any messages about the query in this file as well:
SELECT * FROM Employees; OUTPUT TO Employees.txt APPEND VERBOSE;
Suppose you need to export a value that contains an embedded line feed character. A line feed character has the numeric value 10, which you can represent as the string '\x0a' in a SQL statement. If you execute the following statement, with HEXADECIMAL set to ON,
SELECT 'line1\x0aline2'; OUTPUT TO file.txt HEXADECIMAL ON;
you get a file with one line in it containing the following text:
line10x0aline2
But if you execute the same statement with HEXADEMICAL set to OFF, you get the following:
line1\x0aline2
Finally, if you set HEXADECIMAL to ASIS, you get a file with two lines:
line1 line2
You get two lines when you use ASIS because the embedded line feed character has been exported without being converted to a two digit hex representation, and without being prefixed by anything.