View Definition Viewer (dview.sql)
What This Script Does
Section titled “What This Script Does”This script provides comprehensive view analysis by:
- Displaying complete view definitions (SQL text)
- Showing view metadata including text length
- Supporting pattern matching for owner and view names
- Setting appropriate formatting for long view definitions
- Organizing output by owner and view name
Script
Section titled “Script”rem dview.sqlremrem linesize = 80set lines 80remset long 100000remttitle 'View Definitions'remcol owner format a12 heading 'OWNER'col view_name format a30 heading 'VIEW NAME'col text_length format 99999 heading 'LENGTH'col text format a80 heading 'TEXT'remselect owner, view_name, text_length, text from sys.dba_views where owner like upper('&owner') and view_name like upper('&view') order by owner, view_name;remset long 80
SQL> @dview.sqlEnter value for owner: HREnter value for view: EMP%
Parameters
Section titled “Parameters”- owner: Schema owner pattern to filter (use % for all)
- view: View name pattern to filter (use % for all)
Required Privileges
Section titled “Required Privileges”- SELECT on SYS.DBA_VIEWS
Sample Output
Section titled “Sample Output”View Definitions
OWNER VIEW NAME LENGTH------------ ------------------------------ ------TEXT--------------------------------------------------------------------------------HR EMP_DETAILS_VIEW 245SELECT e.employee_id, e.first_name, e.last_name, e.email, e.hire_date, e.salary, d.department_name, j.job_titleFROM employees eJOIN departments d ON e.department_id = d.department_idJOIN jobs j ON e.job_id = j.job_idWHERE e.status = 'ACTIVE'
HR EMPLOYEE_SALARY_GRADES 189SELECT e.employee_id, e.last_name, e.salary, CASE WHEN e.salary < 5000 THEN 'Grade 1' WHEN e.salary < 10000 THEN 'Grade 2' WHEN e.salary < 15000 THEN 'Grade 3' ELSE 'Grade 4' END AS salary_gradeFROM employees e
SCOTT EMP_DEPT_VIEW 156SELECT e.empno, e.ename, e.job, e.sal, d.dname, d.locFROM emp e, dept dWHERE e.deptno = d.deptno
Key Output Columns
Section titled “Key Output Columns”- OWNER: Schema that owns the view
- VIEW NAME: Name of the view
- LENGTH: Number of characters in view definition
- TEXT: Complete SQL definition of the view
Understanding View Types
Section titled “Understanding View Types”Simple Views
Section titled “Simple Views”- Based on single table
- Direct column mapping
- Often updatable
Complex Views
Section titled “Complex Views”- Multiple table joins
- Calculated columns
- Aggregate functions
- Usually read-only
Materialized Views
Section titled “Materialized Views”- Physically stored result set
- Periodic refresh
- Performance optimization
Common Use Cases
Section titled “Common Use Cases”-
Documentation
- Document view logic
- Understand data relationships
- Create system documentation
-
Impact Analysis
- Identify view dependencies
- Plan table structure changes
- Assess modification impact
-
Performance Tuning
- Analyze view complexity
- Identify optimization opportunities
- Review join strategies
-
Security Analysis
- Understand data access patterns
- Validate security filtering
- Review column exposure
Advanced Analysis
Section titled “Advanced Analysis”Find complex views:
Section titled “Find complex views:”SELECT owner, view_name, text_lengthFROM dba_viewsWHERE text_length > 1000ORDER BY text_length DESC;
Views with specific tables:
Section titled “Views with specific tables:”SELECT owner, view_nameFROM dba_viewsWHERE UPPER(text) LIKE '%EMPLOYEES%'ORDER BY owner, view_name;
View dependencies:
Section titled “View dependencies:”SELECT owner, name, referenced_owner, referenced_nameFROM dba_dependenciesWHERE type = 'VIEW'AND referenced_type = 'TABLE'AND name = 'EMP_DETAILS_VIEW'ORDER BY referenced_owner, referenced_name;
View Management
Section titled “View Management”Create view with definition visible:
Section titled “Create view with definition visible:”-- Simple viewCREATE VIEW hr.active_employees ASSELECT employee_id, first_name, last_name, emailFROM employeesWHERE status = 'ACTIVE';
-- Complex view with joinsCREATE VIEW hr.employee_summary ASSELECT e.employee_id, e.last_name, d.department_name, j.job_title, e.salaryFROM employees eJOIN departments d ON e.department_id = d.department_idJOIN jobs j ON e.job_id = j.job_id;
Modify view definition:
Section titled “Modify view definition:”CREATE OR REPLACE VIEW hr.active_employees ASSELECT employee_id, first_name, last_name, email, hire_dateFROM employeesWHERE status = 'ACTIVE'AND hire_date > DATE '2020-01-01';
Performance Considerations
Section titled “Performance Considerations”View Optimization
Section titled “View Optimization”- Oracle rewrites queries using views
- Complex views may impact performance
- Consider materialized views for heavy aggregations
- Index underlying tables appropriately
Best Practices
Section titled “Best Practices”- Keep view definitions readable
- Document complex business logic
- Avoid nested view references
- Use meaningful column aliases
Security Implications
Section titled “Security Implications”Data Access Control
Section titled “Data Access Control”Views can provide:
- Column-level security (hide sensitive columns)
- Row-level security (filter data)
- Computed columns (derived values)
- Data transformation (format changes)
Privilege Management
Section titled “Privilege Management”-- Grant view access without table accessGRANT SELECT ON hr.employee_summary TO app_user;
-- Revoke direct table accessREVOKE SELECT ON hr.employees FROM app_user;
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”- Invalid views: Underlying objects changed
- Performance problems: Complex view logic
- Privilege errors: Missing table permissions
- Data inconsistency: Outdated view logic
Validation Queries
Section titled “Validation Queries”-- Check view validitySELECT owner, object_name, statusFROM dba_objectsWHERE object_type = 'VIEW'AND status = 'INVALID';
-- Test view executionSELECT COUNT(*) FROM hr.employee_summary;