Database Name Query (name.sql)
What This Script Does
Section titled “What This Script Does”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.
The Script
Section titled “The Script”rem name.sqlremttitle 'Database Name'remSELECT nameFROM v$database/
-- Display current database name@name.sql
Parameters
Section titled “Parameters”The script requires no input parameters.
Required Privileges
Section titled “Required Privileges”-- Usually available to all usersSELECT on V$DATABASE
Sample Output
Section titled “Sample Output” Database Name
NAME---------PROD
Common Use Cases
Section titled “Common Use Cases”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
Integration with Other Scripts
Section titled “Integration with Other Scripts”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
Related Information
Section titled “Related Information”The V$DATABASE view contains additional useful information:
Extended Query
SELECT name, db_unique_name, database_role, open_mode, createdFROM v$database;
Database Status
SELECT name, open_mode, database_roleFROM v$database;
Benefits
Section titled “Benefits”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
Performance Impact
Section titled “Performance Impact”- Minimal: Single row query on system view
- Fast: Instantaneous execution
- No Overhead: No impact on database performance
Use in Automation
Section titled “Use in Automation”Shell Scripts
# Get database name in shell scriptsDB_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
Security Considerations
Section titled “Security Considerations”- Generally safe for all users
- No sensitive information exposed
- Read-only operation with no side effects