List names of all tables in a SQL Server 2012 schema
Get all table names in a particular SQL Server 2012 schema with:
Replace 'YourSchema'
with your actual schema name. This statement returns only actual tables, omitting views.
Refined, compressed knowledge for metadata and optimizations
INFORMATION_SCHEMA
views are standardized in SQL databases, offering a robust strategy to access metadata for your database elements. Utilizing INFORMATION_SCHEMA
views over querying system tables directly ensures compatibility and accuracy across different versions and platforms.
For a deeper dive into ownership, creation dates, join INFORMATION_SCHEMA
views with other system views:
Replace N'YourSchema'
with your own schema name keeping in mind that t.is_ms_shipped = 0
filters out system tables.
Always prefix your queries with:
While USE
confirms the correct database is being targeted, GO
ensures batch execution of your commands.
Exploring deeper: scenarios and use cases
Views: include, exclude
Including views:
Excluding views:
Dealing with multiple schemas
When multiple schemas are in play, a proper filter is key:
Merging databases
For cross-database queries, employ fully qualified names and join conditions:
Avoid worst-case scenarios
- Evade querying system tables directly; structure may change in future versions.
- Ensure table aliases for clarity, it's like naming your pets.
- Verify the database and schema context, because nobody likes to get lost.
Was this article helpful?