<<<<<<< HEAD file_write | H2 | SQLDoc
Skip to main content

file_write

1. FILE_WRITE Function

======= file_write | H2 Function | SQLDoc

file_write

1. FILE_WRITE Function

>>>>>>> 9d19b1c8cb744c8e86d5c15b74d8b5f719ad62be

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!');
<<<<<<< HEAD

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: =======

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: >>>>>>> 9d19b1c8cb744c8e86d5c15b74d8b5f719ad62be 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