EXPORT CSV Statement
Exports a script, table, or view to a CSV (comma-separated values)
file. This statement is the scripting equivalent of the visual export wizard accessed via the Export menu.
Syntax
Parameters
- filename: text
The absolute path to the text file to write. The file does not need to exist. If it does exist, by default new
lines will be appended to it. Use the TRUNCATE_EXISTING_FILE
option to overwrite the existing
file.
- table-name: text or identifier (optional)
If provided, this is the name of a table or view in the notebook to export.
- script-name: text or identifier (optional)
If provided, this is the name of a script in the notebook to export. The script will be executed and then its
results will be written to the file. If the script produces multiple data tables, then only the first is
exported.
- select-statement: statement (optional)
If provided, this is a SELECT
statement that provides the rows to write to the file.
Options
- HEADER_ROW: integer (0-1, default: 1)
Indicates whether the first row of the file should be a column header row.
- SEPARATOR: text (default: ',')
Allows separators other than commas. The separator must be a single character (specifically, a single UTF-16 code
unit). To specify a tab separator, use CHAR(9) which uses the ASCII code 9 to produce the tab
character.
-
TRUNCATE_EXISTING_FILE
: integer (0-1, default: 0)
If the output file exists, this option indicates whether the existing file contents should be deleted.
- 0 = Keep existing file data and append new lines
- 1 = Delete existing file data
FILE_ENCODING
: integer (0-65535, default: 0)
Indicates the text encoding to use when writing the text file. Specify 0 to use UTF-8. Any nonzero integer is
treated as a Windows code page number. See Character Encodings in CSV and
Text Files for a list of these code page numbers.
Example
-- In most cases, this is all that is required.
EXPORT CSV 'C:\file.csv' FROM TABLE my_table;
-- Use the SEPARATOR option to write tab-separated files.
EXPORT CSV 'C:\file.tsv' FROM TABLE my_table OPTIONS (SEPARATOR: CHAR(9));