[sql-server] How do I get list of all tables in a database using TSQL?

What is the best way to get the names of all of the tables in a specific database on SQL Server?

This question is related to sql-server tsql database-table

The answer is


exec sp_msforeachtable 'print ''?'''

Using SELECT * FROM INFORMATION_SCHEMA.COLUMNS also shows you all tables and related columns.


Well you can use sys.objects to get all database objects.

 GO
 select * from sys.objects where type_desc='USER_TABLE' order by name
 GO

OR

--  For all tables
select * from INFORMATION_SCHEMA.TABLES 
GO 

  --- For user defined tables
select * from INFORMATION_SCHEMA.TABLES where TABLE_TYPE='BASE TABLE'
GO

  --- For Views
select * from INFORMATION_SCHEMA.TABLES where TABLE_TYPE='VIEW'
GO

SELECT * FROM INFORMATION_SCHEMA.TABLES 

OR

SELECT * FROM Sys.Tables

select * from sysobjects where xtype='U'


SELECT name 
FROM sysobjects 
WHERE xtype='U' 
ORDER BY name;

(SQL Server 2000 standard; still supported in SQL Server 2005.)


SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U' 

SELECT * FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'

SQL Server 2012


--for oracle
select tablespace_name, table_name from all_tables;

This link can provide much more information on this topic


select * from sysobjects where xtype='U'


SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'

Here is a list of other object types you can search for as well:

  • AF: Aggregate function (CLR)
  • C: CHECK constraint
  • D: Default or DEFAULT constraint
  • F: FOREIGN KEY constraint
  • L: Log
  • FN: Scalar function
  • FS: Assembly (CLR) scalar-function
  • FT: Assembly (CLR) table-valued function
  • IF: In-lined table-function
  • IT: Internal table
  • P: Stored procedure
  • PC: Assembly (CLR) stored-procedure
  • PK: PRIMARY KEY constraint (type is K)
  • RF: Replication filter stored procedure
  • S: System table
  • SN: Synonym
  • SQ: Service queue
  • TA: Assembly (CLR) DML trigger
  • TF: Table function
  • TR: SQL DML Trigger
  • TT: Table type
  • U: User table
  • UQ: UNIQUE constraint (type is K)
  • V: View
  • X: Extended stored procedure

SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_TYPE='BASE TABLE' 
ORDER BY TABLE_NAME

Please use this. You will get table names along with schema names:

SELECT SYSSCHEMA.NAME, SYSTABLE.NAME
FROM SYS.tables SYSTABLE
INNER JOIN SYS.SCHEMAS SYSSCHEMA
ON SYSTABLE.SCHEMA_ID = SYSSCHEMA.SCHEMA_ID

SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U' 

The downside of INFORMATION_SCHEMA.TABLES is that it also includes system tables such as dtproperties and the MSpeer_... tables, with no way to tell them apart from your own tables.

I would recommend using sys.objects (the new version of the deprecated sysobjects view), which does support excluding the system tables:

select *
from sys.objects
where type = 'U'      -- User tables
and is_ms_shipped = 0 -- Exclude system tables

SELECT name 
FROM sysobjects 
WHERE xtype='U' 
ORDER BY name;

(SQL Server 2000 standard; still supported in SQL Server 2005.)


SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U' 

exec sp_msforeachtable 'print ''?'''

SELECT * FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'

SQL Server 2012


SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U' 

SELECT name 
FROM sysobjects 
WHERE xtype='U' 
ORDER BY name;

(SQL Server 2000 standard; still supported in SQL Server 2005.)


Well you can use sys.objects to get all database objects.

 GO
 select * from sys.objects where type_desc='USER_TABLE' order by name
 GO

OR

--  For all tables
select * from INFORMATION_SCHEMA.TABLES 
GO 

  --- For user defined tables
select * from INFORMATION_SCHEMA.TABLES where TABLE_TYPE='BASE TABLE'
GO

  --- For Views
select * from INFORMATION_SCHEMA.TABLES where TABLE_TYPE='VIEW'
GO

select * from sysobjects where xtype='U'


USE YourDBName
GO 
SELECT *
FROM sys.Tables
GO

OR

USE YourDBName
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES 
GO

SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'

Here is a list of other object types you can search for as well:

  • AF: Aggregate function (CLR)
  • C: CHECK constraint
  • D: Default or DEFAULT constraint
  • F: FOREIGN KEY constraint
  • L: Log
  • FN: Scalar function
  • FS: Assembly (CLR) scalar-function
  • FT: Assembly (CLR) table-valued function
  • IF: In-lined table-function
  • IT: Internal table
  • P: Stored procedure
  • PC: Assembly (CLR) stored-procedure
  • PK: PRIMARY KEY constraint (type is K)
  • RF: Replication filter stored procedure
  • S: System table
  • SN: Synonym
  • SQ: Service queue
  • TA: Assembly (CLR) DML trigger
  • TF: Table function
  • TR: SQL DML Trigger
  • TT: Table type
  • U: User table
  • UQ: UNIQUE constraint (type is K)
  • V: View
  • X: Extended stored procedure

SELECT * FROM INFORMATION_SCHEMA.TABLES 

OR

SELECT * FROM Sys.Tables

SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_TYPE='BASE TABLE' 
ORDER BY TABLE_NAME

exec sp_msforeachtable 'print ''?'''

SELECT * FROM INFORMATION_SCHEMA.TABLES 

OR

SELECT * FROM Sys.Tables

Thanks to Ray Vega, whose response gives all user tables in a database...

exec sp_msforeachtable 'print ''?'''

sp_helptext shows the underlying query, which summarises to...

select * from dbo.sysobjects o 
join sys.all_objects syso on o.id =  syso.object_id  
where OBJECTPROPERTY(o.id, 'IsUserTable') = 1 
and o.category & 2 = 0 

Using SELECT * FROM INFORMATION_SCHEMA.COLUMNS also shows you all tables and related columns.


SELECT name 
FROM sysobjects 
WHERE xtype='U' 
ORDER BY name;

(SQL Server 2000 standard; still supported in SQL Server 2005.)


SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'

Here is a list of other object types you can search for as well:

  • AF: Aggregate function (CLR)
  • C: CHECK constraint
  • D: Default or DEFAULT constraint
  • F: FOREIGN KEY constraint
  • L: Log
  • FN: Scalar function
  • FS: Assembly (CLR) scalar-function
  • FT: Assembly (CLR) table-valued function
  • IF: In-lined table-function
  • IT: Internal table
  • P: Stored procedure
  • PC: Assembly (CLR) stored-procedure
  • PK: PRIMARY KEY constraint (type is K)
  • RF: Replication filter stored procedure
  • S: System table
  • SN: Synonym
  • SQ: Service queue
  • TA: Assembly (CLR) DML trigger
  • TF: Table function
  • TR: SQL DML Trigger
  • TT: Table type
  • U: User table
  • UQ: UNIQUE constraint (type is K)
  • V: View
  • X: Extended stored procedure

In SSMS, to get all fully qualified table names in a specific database (E.g., "MyDatabase"):

SELECT [TABLE_CATALOG] + '.' + [TABLE_SCHEMA] + '.' + [TABLE_NAME]
FROM   MyDatabase.INFORMATION_SCHEMA.Tables
WHERE  [TABLE_TYPE] = 'BASE TABLE' and [TABLE_NAME] <> 'sysdiagrams'
ORDER BY [TABLE_SCHEMA], [TABLE_NAME]

Results:

  • MyDatabase.dbo.MyTable1
  • MyDatabase.dbo.MyTable2
  • MyDatabase.MySchema.MyTable3
  • MyDatabase.MySchema.MyTable4
  • etc.

In SSMS, to get all fully qualified table names in a specific database (E.g., "MyDatabase"):

SELECT [TABLE_CATALOG] + '.' + [TABLE_SCHEMA] + '.' + [TABLE_NAME]
FROM   MyDatabase.INFORMATION_SCHEMA.Tables
WHERE  [TABLE_TYPE] = 'BASE TABLE' and [TABLE_NAME] <> 'sysdiagrams'
ORDER BY [TABLE_SCHEMA], [TABLE_NAME]

Results:

  • MyDatabase.dbo.MyTable1
  • MyDatabase.dbo.MyTable2
  • MyDatabase.MySchema.MyTable3
  • MyDatabase.MySchema.MyTable4
  • etc.

SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'

Here is a list of other object types you can search for as well:

  • AF: Aggregate function (CLR)
  • C: CHECK constraint
  • D: Default or DEFAULT constraint
  • F: FOREIGN KEY constraint
  • L: Log
  • FN: Scalar function
  • FS: Assembly (CLR) scalar-function
  • FT: Assembly (CLR) table-valued function
  • IF: In-lined table-function
  • IT: Internal table
  • P: Stored procedure
  • PC: Assembly (CLR) stored-procedure
  • PK: PRIMARY KEY constraint (type is K)
  • RF: Replication filter stored procedure
  • S: System table
  • SN: Synonym
  • SQ: Service queue
  • TA: Assembly (CLR) DML trigger
  • TF: Table function
  • TR: SQL DML Trigger
  • TT: Table type
  • U: User table
  • UQ: UNIQUE constraint (type is K)
  • V: View
  • X: Extended stored procedure

The downside of INFORMATION_SCHEMA.TABLES is that it also includes system tables such as dtproperties and the MSpeer_... tables, with no way to tell them apart from your own tables.

I would recommend using sys.objects (the new version of the deprecated sysobjects view), which does support excluding the system tables:

select *
from sys.objects
where type = 'U'      -- User tables
and is_ms_shipped = 0 -- Exclude system tables

USE YourDBName
GO 
SELECT *
FROM sys.Tables
GO

OR

USE YourDBName
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES 
GO

--for oracle
select tablespace_name, table_name from all_tables;

This link can provide much more information on this topic


Thanks to Ray Vega, whose response gives all user tables in a database...

exec sp_msforeachtable 'print ''?'''

sp_helptext shows the underlying query, which summarises to...

select * from dbo.sysobjects o 
join sys.all_objects syso on o.id =  syso.object_id  
where OBJECTPROPERTY(o.id, 'IsUserTable') = 1 
and o.category & 2 = 0 

exec sp_msforeachtable 'print ''?'''

SELECT * FROM INFORMATION_SCHEMA.TABLES 

OR

SELECT * FROM Sys.Tables

Examples related to sql-server

Passing multiple values for same variable in stored procedure SQL permissions for roles Count the Number of Tables in a SQL Server Database Visual Studio 2017 does not have Business Intelligence Integration Services/Projects ALTER TABLE DROP COLUMN failed because one or more objects access this column Create Local SQL Server database How to create temp table using Create statement in SQL Server? SQL Query Where Date = Today Minus 7 Days How do I pass a list as a parameter in a stored procedure? SQL Server date format yyyymmdd

Examples related to tsql

Passing multiple values for same variable in stored procedure Count the Number of Tables in a SQL Server Database Change Date Format(DD/MM/YYYY) in SQL SELECT Statement Stored procedure with default parameters Format number as percent in MS SQL Server EXEC sp_executesql with multiple parameters SQL Server after update trigger How to compare datetime with only date in SQL Server Text was truncated or one or more characters had no match in the target code page including the primary key in an unpivot Printing integer variable and string on same line in SQL

Examples related to database-table

Count the Number of Tables in a SQL Server Database SQL count rows in a table Mysql: Select rows from a table that are not in another Import CSV to mysql table MySQL > Table doesn't exist. But it does (or it should) Create table in SQLite only if it doesn't exist already Copy a table from one database to another in Postgres Truncating all tables in a Postgres database Maximum number of records in a MySQL database table Why use multiple columns as primary keys (composite primary key)