totn Oracle / PLSQL

Oracle / PLSQL: Retrieve the name of the Oracle instance currently connected to

Question: How can I get the name of the Oracle database instance that I'm connected to through a SQL statement?

Answer: You can retrieve the instance name and database name using the sys_context function.

Instance Name

To retrieve the Oracle instance name, you execute the following SQL statement:

SELECT sys_context('USERENV','INSTANCE_NAME') AS Instance
FROM dual;

It should return something like this:

INSTANCE
--------------------------------------------------------------------------------------
TOTN

Database Name

You could also retrieve the Oracle database name with the following SQL statement:

SELECT sys_context('USERENV','DB_NAME') AS Database_Name
FROM dual;

It should return something like this:

DATABASE_NAME
--------------------------------------------------------------------------------------
DBPROD