How do I get list of all tables in a database using TSQL?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Transact-SQL (T-SQL) is the primary means through which both developers and database administrators interact with SQL Server databases. When managing a SQL Server database, a common task is to obtain a list of all tables within the database. This is useful for a variety of purposes including database auditing, migrations, or maintenance tasks. Below, we delve into how to achieve this using T-SQL with practical examples and deeper insights into SQL Server system views.
Understanding SQL Server Catalog Views
SQL Server provides several system catalog views that store information about all database objects, including tables. These catalog views are essential tools for querying meta data about the database structure. Two of the primary views used to retrieve table information are:
sys.tables: Provides a row for each table in the database. This view includes information about the table itself but not its columns or rows.INFORMATION_SCHEMA.TABLES: This is a part of the ANSI standard and offers a cross-RDBMS way of querying meta-data. It provides information about all tables in the database.
Using sys.tables
The sys.tables view provides more detailed information specific to SQL Server. This is often the preferred method when working within a purely SQL Server environment. Here's how to use it:
In this query:
namereturns the table's name.schema_idcan be joined tosys.schemasto find the schema name.create_dateshows when the table was created.
You might often need the schema name along with the table's name to fully qualify the table, which is very important in databases having multiple schemas:
Using INFORMATION_SCHEMA.TABLES
For those working in environments that might include multiple types of database systems or for developers looking for ANSI standard methods, INFORMATION_SCHEMA.TABLES is ideal.
This query filters out views and returns only the tables:
TABLE_SCHEMA- the schema of the tableTABLE_NAME- the name of the tableTABLE_TYPE- type of the object, which here is filtered to show only 'BASE TABLE'.
Practical Considerations
When you're tasked with managing or auditing large databases with many objects, it is practical to also consider:
- Filtering and sorting results: As shown, you may want to order the results or filter them, for instance by schema.
- Considering permissions: Be aware that viewing metadata might be subject to permissions. Ensure the executing account has appropriate rights.
Here is a brief table that summarizes the key SQL views and commands discussed:
| SQL View / Command | Use Case | Remarks |
sys.tables | SQL Server-specific view of tables | Provides detailed info about tables |
INFORMATION_SCHEMA.TABLES | ANSI standard view of table metadata | Compatible across multiple RDBMS |
TABLE_SCHEMA | Schema name in INFORMATION_SCHEMA | Useful for fully qualifying table names |
TABLE_NAME | Table name in both views | Essential for identifying the table |
TABLE_TYPE | Type of the table in INFORMATION_SCHEMA | Useful to distinguish between table types |
Conclusion
By using the sys.tables and INFORMATION_SCHEMA.TABLES views, developers and DBAs can effectively manage and interact with the tables in their SQL Server databases. Each method has its own advantages depending on the specific requirements and environment. The examples provided here are fundamental and can be expanded with more complex SQL clauses as needed, based on the scope of the database management tasks at hand.

