USE DATABASE statement in allows users to specify the database context for their queries within a session. This statement is crucial for operations within Firebolt where users interact with multiple databases, ensuring queries are executed against the intended database without the need to fully qualify object names in their SQL statements.
Syntax
Parameters
| Parameter | Description |
|---|---|
DATABASE | This is an optional keyword. |
<database_name> | The target database for session context. Essential for directing queries to the correct database without specifying the database name in each query. The database name must adhere to identifier rules supported by Firebolt, allowing for quoted or unquoted identifiers. An error is thrown if the database does not exist or the user lacks the necessary permissions. |
USE DATABASE is designed to set the context at a session level, it plays a crucial role in the resolution of unqualified object names. All subsequent queries in the session resolve unqualified single-part names (like table or function names) based on the set database context, unless explicitly overridden by using fully qualified names or other USE statements is encountered.
Example
Set the current database context to the ‘sales’ database:Using USE DATABASE with an engine’s URL
When issuing a successful USE DATABASE statement using an engine’s URL (see information_schema.engines), Firebolt returns the Firebolt-Update-Parameters response header containing the new parameters that should be appended to the HTTP query string for subsequent statements to use the newly selected database.
FAQs
-
What is the purpose of the
USE DATABASEcommand?
TheUSE DATABASEcommand sets the current database context for the session, ensuring that all subsequent queries operate within the specified database unless anotherUSEcommand changes the context. -
How does the
USE DATABASEcommand affect the resolution of unqualified object names in queries?
Once theUSE DATABASEcommand is executed, all unqualified single-part names or two-part names, such as table or function names, in subsequent queries are resolved based on the set database context, simplifying queries by not requiring full names. -
Can I access objects in different databases without changing the session’s database context after using
USE DATABASE?
Yes, you can access objects in different databases by using fully qualified names (including the database name and schema name) in your queries, which allows you to bypass the current session’s database context. -
What happens if I use another
USE DATABASEcommand within the same session?
Executing anotherUSE DATABASEcommand changes the session’s database context to the new database specified, affecting the resolution of unqualified object names in all subsequent queries. -
How do I ensure my query targets the correct database and object if I have used the
USE DATABASEcommand?
To target a specific database and object, you can use fully qualified names in your queries regardless of the current session’s database context, or ensure theUSE DATABASEcommand has set the desired database before executing unqualified queries. -
Can I use
USE DATABASEwithin a transaction?
Yes, but it will only affect the database context for the duration of the transaction.