Table Owner Lookup (towner.sql)
What This Script Does
Section titled “What This Script Does”This script provides Oracle database functionality via the towner.sql script.
The Script
Section titled “The Script”rem towner.sqlremttitle 'Table Owners'remcol name format a41 heading 'OWNER'remselect DISTINCT OWNER from DBA_TABLES where TABLE_NAME like UPPER('&name') order by OWNER;
What This Script Does
Section titled “What This Script Does”This simple but essential utility script helps locate which schemas contain tables with a specific name or name pattern. It’s particularly useful in environments with multiple schemas where table names might be duplicated across different owners, or when you need to find all instances of tables matching a pattern.
Key Features
Section titled “Key Features”- Cross-Schema Search: Searches all schemas in the database
- Pattern Matching: Supports SQL wildcards for flexible table name searches
- Distinct Results: Shows unique schema owners only
- Case Insensitive: Automatically converts search pattern to uppercase
- Quick Reference: Fast way to locate table ownership
Run the script and provide a table name pattern when prompted:
@towner.sql
Input Parameters:
- Table Name: Table name or pattern (supports % and _ wildcards)
Required Privileges
Section titled “Required Privileges”SELECT on DBA_TABLES
Sample Output
Section titled “Sample Output” Table Owners
OWNER-----------------------------------------FINANCEHRPAYROLLREPORTINGSCOTT
Common Usage Examples
Section titled “Common Usage Examples”Find Exact Table Name
Section titled “Find Exact Table Name”Enter value for name: EMPLOYEES
Shows all schemas containing a table named “EMPLOYEES”
Find Tables Starting with Pattern
Section titled “Find Tables Starting with Pattern”Enter value for name: EMP%
Shows schemas with tables starting with “EMP”
Find Tables Ending with Pattern
Section titled “Find Tables Ending with Pattern”Enter value for name: %_HIST
Shows schemas with tables ending in “_HIST”
Find Tables Containing Pattern
Section titled “Find Tables Containing Pattern”Enter value for name: %CUSTOMER%
Shows schemas with tables containing “CUSTOMER”
Use Cases
Section titled “Use Cases”-
Table Location
- Find which schema contains a specific table
- Locate tables across multiple environments
-
Schema Analysis
- Identify duplicate table names across schemas
- Understand table distribution patterns
-
Migration Planning
- Inventory table ownership before migrations
- Identify schema dependencies
-
Security Auditing
- Find all schemas with sensitive table names
- Audit table access across schemas
-
Development Support
- Help developers find the correct schema for tables
- Resolve ambiguous table references
Analysis Examples
Section titled “Analysis Examples”Finding All Customer-Related Tables
Section titled “Finding All Customer-Related Tables”@towner.sqlEnter value for name: %CUSTOMER%
Result shows all schemas with customer tables:HRSALESFINANCEMARKETING
Locating Audit Tables
Section titled “Locating Audit Tables”@towner.sqlEnter value for name: %AUDIT%
Result shows schemas with audit tables:SECURITYCOMPLIANCEAPP_SCHEMA
Finding Temporary Tables
Section titled “Finding Temporary Tables”@towner.sqlEnter value for name: TEMP_%
Result shows schemas with temporary tables:ETL_SCHEMADATA_WAREHOUSESTAGING
Advanced Usage
Section titled “Advanced Usage”Combine with Other Queries
Section titled “Combine with Other Queries”After finding owners, you can drill down further:
-- After running towner.sql, check table detailsSELECT owner, table_name, num_rows, last_analyzedFROM dba_tablesWHERE table_name LIKE 'EMPLOYEES%'AND owner IN ('HR', 'PAYROLL');
Integration with Schema Analysis
Section titled “Integration with Schema Analysis”-- Find all tables in schemas identified by towner.sqlSELECT table_name, num_rowsFROM dba_tablesWHERE owner = 'HR'ORDER BY table_name;
Troubleshooting
Section titled “Troubleshooting”No Results Returned
- Check if table name is spelled correctly
- Verify the table exists in the database
- Ensure you have privileges to see DBA_TABLES
Too Many Results
- Use more specific patterns to narrow the search
- Consider exact table names instead of wildcards
Missing Expected Schemas
- Some schemas might not be visible due to privilege restrictions
- Check if tables are actually tables (not views or synonyms)
Performance Considerations
Section titled “Performance Considerations”- DBA_TABLES is a data dictionary view that’s generally fast
- Using wildcards may slow down the query on very large databases
- Results are cached by Oracle’s dictionary cache
Related Scripts
Section titled “Related Scripts”- dobj.sql - Complete object listing with status
- dtable.sql - Detailed table analysis
- dobjectf.sql - Objects with timestamps
- duser.sql - User account analysis
Script Variations
Section titled “Script Variations”Enhanced Version with Table Count
Section titled “Enhanced Version with Table Count”SELECT owner, COUNT(*) table_countFROM dba_tablesWHERE table_name LIKE UPPER('&name')GROUP BY ownerORDER BY owner;
With Table Size Information
Section titled “With Table Size Information”SELECT DISTINCT ownerFROM dba_tablesWHERE table_name LIKE UPPER('&name')AND num_rows > 0 -- Only tables with dataORDER BY owner;
Include Synonyms
Section titled “Include Synonyms”SELECT DISTINCT table_ownerFROM dba_synonymsWHERE synonym_name LIKE UPPER('&name')UNIONSELECT DISTINCT ownerFROM dba_tablesWHERE table_name LIKE UPPER('&name')ORDER BY 1;