Herman Code 🚀

How do I get list of all tables in a database using TSQL

February 20, 2025

How do I get list of all tables in a database using TSQL

Running with databases frequently requires a broad knowing of its construction. A cardinal project for immoderate SQL Server developer oregon head is retrieving a database of each tables inside a circumstantial database. Figuring out however to effectively get this accusation is important for duties ranging from information investigation and reporting to schema direction and exertion improvement. This station volition dive heavy into however to retrieve a database of each tables successful a SQL Server database utilizing Transact-SQL (TSQL), providing assorted strategies and explaining the nuances of all. We’ll research the intricacies of scheme tables, supply applicable examples, and equip you with the cognition to confidently navigate your SQL Server databases.

Exploring Scheme Tables: The INFORMATION_SCHEMA Attack

1 of the about communal and standardized methods to retrieve array accusation is by querying the INFORMATION_SCHEMA views. These views supply a accordant manner to entree metadata astir database objects careless of the underlying SQL Server interpretation. Particularly, the INFORMATION_SCHEMA.TABLES position is our mark.

Present’s however you tin usage it:

Choice TABLE_NAME FROM INFORMATION_SCHEMA.TABLES Wherever TABLE_TYPE = 'Basal Array'; 

The Wherever clause with TABLE_TYPE = 'Basal Array' filters the outcomes to entertainment lone person-outlined tables, excluding scheme tables and views. This ensures you acquire a cleanable database of the tables you’re about apt curious successful.

Leveraging the sys.tables Catalog Position: A Much Almighty Attack

For much elaborate accusation and larger flexibility, the sys.tables catalog position gives a richer dataset. This position is circumstantial to SQL Server and presents entree to much properties and particulars astir all array.

The pursuing question retrieves the names of each person tables inside the actual database:

Choice sanction FROM sys.tables Wherever kind = 'U'; 

Present, kind = 'U' filters for person tables. sys.tables besides accommodates scheme tables; utilizing the filter ensures you direction connected person-created tables. This attack is mostly most well-liked for its show and entree to further array properties similar instauration day, modification day, and schema possession.

Filtering Outcomes: Focusing on Circumstantial Schemas

Databases frequently form tables into antithetic schemas, offering logical groupings for objects. You mightiness privation to database tables lone inside a peculiar schema. Some INFORMATION_SCHEMA and sys.tables let schema-based mostly filtering.

Utilizing INFORMATION_SCHEMA:

Choice TABLE_NAME FROM INFORMATION_SCHEMA.TABLES Wherever TABLE_SCHEMA = 'dbo' AND TABLE_TYPE = 'Basal Array'; 

Utilizing sys.tables (on with sys.schemas):

Choice t.sanction FROM sys.tables t Articulation sys.schemas s Connected t.schema_id = s.schema_id Wherever s.sanction = 'dbo' AND t.kind = 'U'; 

These queries retrieve tables particularly inside the dbo schema. Regenerate 'dbo' with the desired schema sanction for focused retrieval.

Precocious Filtering and Sorting: Customizing Your Output

You tin additional refine your queries to kind and filter based mostly connected circumstantial standards. For case, you mightiness privation to command tables alphabetically oregon filter by instauration day. TSQL gives the flexibility to customise your output primarily based connected your wants.

Present’s an illustration utilizing sys.tables to command tables by sanction:

Choice sanction FROM sys.tables Wherever kind = 'U' Command BY sanction ASC; 

This question retrieves person tables sorted alphabetically successful ascending command. You tin modify the Command BY clause to kind by another properties disposable successful sys.tables, specified arsenic create_date oregon modify_date. Research the SQL Server documentation for a blanket database of disposable properties.

Infographic Placeholder: Ocular cooperation of querying scheme tables.

  • Utilizing INFORMATION_SCHEMA supplies a standardized attack.
  • sys.tables affords much elaborate accusation and amended show.
  1. Link to your SQL Server database.
  2. Execute the due TSQL question.
  3. Analyse the returned database of tables.

In accordance to a study by Stack Overflow, SQL Server stays 1 of the about fashionable database platforms. Knowing however to navigate its construction is a invaluable accomplishment for immoderate information nonrecreational.

Uncovering the correct instruments and methods for database direction is important. By mastering these TSQL queries, you tin effectively research and negociate your SQL Server databases. These strategies supply a strong instauration for assorted database duties, from schema investigation to exertion improvement. Whether or not you like the standardized attack of INFORMATION_SCHEMA oregon the elaborate accusation offered by sys.tables, take the technique that champion fits your circumstantial wants. Retrieve to see filtering by schema and customizing your queries for sorting and further standards. This cognition volition undoubtedly streamline your workflow and heighten your quality to activity efficaciously with SQL Server databases.

  • Schema filtering allows focused array retrieval.
  • Sorting and filtering heighten consequence customization.

FAQ:

Q: What is the quality betwixt INFORMATION_SCHEMA and sys.tables?

A: INFORMATION_SCHEMA is an ANSI modular fit of views, offering a accordant manner to entree database metadata crossed antithetic database techniques. sys.tables is a SQL Server-circumstantial catalog position, providing much elaborate accusation and frequently amended show.

For additional speechmaking connected associated subjects, research assets connected SQL Server scheme tables, database direction champion practices, and TSQL question optimization. Cheque retired these adjuvant sources: Microsoft SQL Server Documentation, Stack Overflow SQL Server Tag, and Brent Ozar Limitless’s Weblog.

Question & Answer :
What is the champion manner to acquire the names of each of the tables successful a circumstantial database connected SQL Server?

SQL Server 2000, 2005, 2008, 2012, 2014, 2016, 2017 oregon 2019:

Choice * FROM INFORMATION_SCHEMA.TABLES Wherever TABLE_TYPE='Basal Array' 

To entertainment lone tables from a peculiar database

Choice TABLE_NAME FROM [<DATABASE_NAME>].INFORMATION_SCHEMA.TABLES Wherever TABLE_TYPE = 'Basal Array' 

Oregon,

Choice TABLE_NAME FROM INFORMATION_SCHEMA.TABLES Wherever TABLE_TYPE = 'Basal Array' AND TABLE_CATALOG='dbName' --(for MySql, usage: TABLE_SCHEMA='dbName' ) 

PS: For SQL Server 2000:

Choice * FROM sysobjects Wherever xtype='U'