In Microsoft SQL Server, you can list tables in a few different ways depending on your environment (SQL Server Management Studio, command line, etc.). Here’s how you can do it:
Using SQL Server Management Studio (SSMS)
- Connect to your SQL Server instance in SSMS.
- Expand the database where you want to view the tables.
- Expand the "Tables" folder under the database. This will show you a list of all tables in that database.
Using T-SQL Query
You can use T-SQL queries to list tables in a database. Here are a few methods:
-
List Tables in the Current Database
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'prefix%';This query selects all rows from the
INFORMATION_SCHEMA.TABLES
view whereTABLE_TYPE
is 'BASE TABLE'. This will show you all the tables in the current database. -
List Tables with Schema Information
SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE';This query also includes the schema of each table, which can be useful if you have multiple schemas.
-
Using sys.tables
SELECT name FROM sys.tables;The
sys.tables
system catalog view returns one row for each table object in the database. -
Using sysobjects (Legacy)
SELECT name FROM sysobjects WHERE type = 'U'; -- 'U' stands for user tablesNote that
sysobjects
is a legacy system table; it’s generally recommended to usesys.tables
for newer code.
Filtering Tables
You can also filter the tables based on names or other criteria:
Replace 'prefix%'
with the specific table name or pattern you are interested in.