Skip to main content

current_schema

1. CURRENT_SCHEMA Function

The CURRENT_SCHEMA function in H2 database is used to retrieve the name of the current schema or the default schema set for the current session.

2. Syntax

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

CURRENT_SCHEMA()

Arguments

  • The CURRENT_SCHEMA function does not require any arguments.

Return

  • The CURRENT_SCHEMA function returns the name of the current schema as a string.

3. Notes

  • The CURRENT_SCHEMA function in H2 database returns the name of the schema as it is set for the current session. If no schema is explicitly set, it returns the default schema for the database connection.
  • The current schema name can be used to reference tables, views, and other database objects without fully qualifying their names.
  • In H2 database, the current schema is set using the SET SCHEMA statement or by specifying the schema name in the table or view names.

4. Examples

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

Example 1 - Retrieving the current schema name:

SELECT CURRENT_SCHEMA() AS current_schema;

Output:

current_schema
--------------
PUBLIC

Example 2 - Using the current schema to reference a table:

SET SCHEMA TEST;

CREATE TABLE test_table (
id INT PRIMARY KEY,
name VARCHAR(50)
);

-- Inserting data into the table
INSERT INTO test_table VALUES (1, 'John'), (2, 'Jane');

-- Querying the table using the current schema
SELECT * FROM test_table;

Output:

id | name
---+------
1 | John
2 | Jane
  • SET SCHEMA - Set the current schema for the session
  • SCHEMAS - Retrieve the list of all available schemas