USE SCHEMA

Since: Databricks Runtime 10.2

Sets the current schema. After the current schema is set, unqualified references to objects such as tables, functions, and views that are referenced by SQLs are resolved from the current schema. The default schema name is default.

While usage of SCHEMA and DATABASE is interchangeable, SCHEMA is preferred.

Syntax

USE [SCHEMA] schema_name

Parameter

  • schema_name

    Name of the schema to use. If schema_name is qualified the current catalog is also set to the specified catalog name. If the schema does not exist, an exception is thrown.

Examples

-- Use the 'userschema' which exists.
> USE SCHEMA userschema;

-- Use the 'userschema1' which doesn't exist
> USE SCHEMA userschema1;
  Error: Database 'userschema1' not found;

-- Setting the catalog resets the schema to `default`
> USE CATALOG some_cat;
> SELECT current_schema(), current_catalog();
  some_cat default

-- Setting the schema within the current catalog
> USE SCHEMA some_schem;
> SELECT current_schema(), current_catalog();
  some_cat some_schema

-- Resetting both catalog and schema
> USE SCHEMA main.my_schema;
> SELECT current_schema(), current_catalog();
  main my_schema

-- Setting the catalog resets the schema to `default` again
> USE CATALOG some_cat;
> SELECT current_schema(), current_catalog();
  some_cat default