Skip to main content

file_write

1. FILE_WRITE Function

The FILE_WRITE function in H2 database is used to write data to a file.

2. Syntax

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

FILE_WRITE(file, data)

Arguments

  • file: The name or path of the file to be written. It should be a string (VARCHAR) value.
  • data: The data to be written to the file. It should be a string (VARCHAR) value.

Return

  • The FILE_WRITE function does not return any value.

3. Notes

  • The FILE_WRITE function in H2 database allows you to write data to a file on the server-side. It is typically used for file I/O operations within the database.
  • The file specified in the file argument should either be an absolute path or a path relative to the database server's working directory.
  • The database user executing the FILE_WRITE function should have appropriate write permissions on the specified file and directory.
  • If the file does not exist, it will be created. If the file already exists, the existing content will be overwritten.
  • Remember to use the correct syntax and handle file-related errors appropriately while using the FILE_WRITE function.

4. Examples

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

Example 1 - Writing data to a file:

FILE_WRITE('output.txt', 'Hello, World!');

Output: The content "Hello, World!" is written to the file output.txt.

Example 2 - Writing data to a file using a column value:

CREATE TABLE messages (
id INT PRIMARY KEY,
content VARCHAR
);

INSERT INTO messages VALUES (1, 'This is a sample message.');

SELECT id, content, FILE_WRITE('output.txt', content) AS result
FROM messages;

Output: The content of the content column is written to the file output.txt, and the result column will have a NULL value for each row.

  • file_read - Read data from a file
  • FILE_EXISTS - Check if a file exists