Skip to content

Database Name Query (name.sql)

Displays the name of the current database by querying the V$DATABASE view. This simple but essential script provides quick confirmation of which database you’re connected to.

rem name.sql
rem
ttitle 'Database Name'
rem
SELECT name
FROM v$database
/
-- Display current database name
@name.sql

The script requires no input parameters.

-- Usually available to all users
SELECT on V$DATABASE
Database Name
NAME
---------
PROD

Environment Verification

-- Confirm you're connected to the correct database
@name.sql
-- Especially important in production environments

Script Documentation

-- Include in larger scripts to document target database
@name.sql
-- Provides audit trail in spool files

Connection Validation

-- Quick check after establishing database connection
@name.sql
-- Verify connection success and target

Multi-Environment Management

-- Distinguish between DEV, TEST, and PROD databases
@name.sql
-- Essential for DBAs managing multiple environments

Login Scripts

-- Include in login.sql for automatic display
@name.sql
-- Shows database name at session start

Health Check Scripts

-- Include in comprehensive health checks
@name.sql
-- Documents which database was analyzed

Batch Processing

-- Verify target database before bulk operations
@name.sql
-- Prevent accidental operations on wrong database

The V$DATABASE view contains additional useful information:

Extended Query

SELECT name,
db_unique_name,
database_role,
open_mode,
created
FROM v$database;

Database Status

SELECT name,
open_mode,
database_role
FROM v$database;

Safety Check

  • Prevents accidental operations on wrong database
  • Quick environment verification
  • Essential for production safety

Documentation

  • Provides clear database identification
  • Useful in log files and reports
  • Audit trail for operations

Simplicity

  • Minimal resource usage
  • Fast execution
  • Universal compatibility
  • Minimal: Single row query on system view
  • Fast: Instantaneous execution
  • No Overhead: No impact on database performance

Shell Scripts

Terminal window
# Get database name in shell scripts
DB_NAME=$(sqlplus -s user/pass@db <<< "SELECT name FROM v$database;" | grep -v "^$")

Batch Files

-- Include at start of batch processing scripts
@name.sql
-- Confirm correct target database
  • Generally safe for all users
  • No sensitive information exposed
  • Read-only operation with no side effects
  • login - SQL*Plus login configuration
  • db - Comprehensive database information
  • gvinst - RAC instance status