Skip to main content

csvwrite

1. CSVWRITE Function

The CSVWRITE function in H2 database is used to write the result of a query to a CSV (Comma-Separated Values) file.

2. Syntax

The syntax for the CSVWRITE function in H2 database is as follows:

CSVWRITE(fileName, query, [options])

Arguments

  • fileName: The name of the CSV file to be created. It can be an absolute or relative path.
  • query: The SELECT query whose result will be written to the CSV file.
  • options (optional): Additional options that can be specified.

Return

  • The CSVWRITE function does not return any value. It generates a CSV file with the query result.

3. Notes

  • The CSVWRITE function creates a new CSV file or overwrites an existing file with the same name.
  • The query passed to the CSVWRITE function should be a valid SELECT query, and the columns in the SELECT statement will be written as CSV columns.
  • By default, the CSV file will use a comma (,) as the field delimiter and double quotes (") as the quote character.
  • The CSV file will have a header row containing the column names from the query result.
  • The CSV file will be written in the default character encoding of the database.

4. Examples

Here are a few examples demonstrating the usage of the CSVWRITE function in H2 database:

Example 1 - Writing the result of a query to a CSV file:

CSVWRITE('output.csv', 'SELECT * FROM employees');

Output: A CSV file named output.csv will be created in the current directory containing the result of the query SELECT * FROM employees.

Example 2 - Specifying options for CSV generation:

CSVWRITE('output.csv', 'SELECT name, age, salary FROM employees', 'FIELDDELIMITER=;, QUOTECHAR=\'');

Output: A CSV file named output.csv will be created in the current directory using a semicolon (;) as the field delimiter and a single quote (') as the quote character. The file will contain the columns name, age, and salary from the employees table.

  • csvread - Read data from a CSV file into a table.