1097

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

3

20 Answers 20

1700

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

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'

To show only tables from a particular database

SELECT TABLE_NAME 
FROM [<DATABASE_NAME>].INFORMATION_SCHEMA.TABLES 
WHERE TABLE_TYPE = 'BASE TABLE'

Or,

SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_TYPE = 'BASE TABLE' 
    AND TABLE_CATALOG='dbName' --(for MySql, use: TABLE_SCHEMA='dbName' )

PS: For SQL Server 2000:

SELECT * FROM sysobjects WHERE xtype='U' 
8
  • 18
    Add the database name if you are not using the specific database so it will be SELECT TABLE_NAME FROM <DATABASE_NAME>.INFORMATION_SCHEMA.Tables
    – Shriroop
    Commented Aug 16, 2013 at 9:28
  • 24
    Adding WHERE TABLE_TYPE='BASE TABLE' will include only base tables (and by extension you could always use WHERE TABLE_TYPE != 'VIEW'). Commented Aug 1, 2014 at 17:45
  • 4
    "sysdiagrams" appears in this list too :(
    – celsowm
    Commented Nov 24, 2014 at 17:11
  • 4
    sysdiagrams is a normal table, you always have to exclude it manually with a AND name <> 'sysdiagrams'.
    – Christoph
    Commented Jun 22, 2015 at 8:57
  • and if one wish to list only table names, one would use name in place of the * like below select name from sysobjects where xtype='U' and name ,. 'sysdiagrams'
    – gg89
    Commented Jul 1, 2015 at 6:32
211
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
4
  • 13
    The aliasing is a bit redundant: SELECT name FROM sysobjects WHERE xtype = 'U' would do the same thing. Commented Jun 30, 2015 at 3:24
  • Thanks, initially i tried this with multiple select statements for PK,FK,D,C,V,UQ etc to compare source and target database, but then i found this feature in VS, but is there not a sql query to compare complete source and target database ?
    – Shaiju T
    Commented Dec 3, 2015 at 11:29
  • One wonders why 'U'is used to identify the User Table... as opposed to maybe 'UT' or, the most intuitive, 'T'...Ah well, this works!
    – tinonetic
    Commented Mar 2, 2017 at 4:51
  • Is there a way to query the crosswalk of object types to type names so it could be easily joined?
    – Mark E.
    Commented May 27, 2022 at 23:41
120
SELECT * FROM INFORMATION_SCHEMA.TABLES 

OR

SELECT * FROM Sys.Tables
2
  • 6
    Just a note that (as mentioned in other answers) sys.tables is only available in 2005 onwards
    – Rob
    Commented Oct 6, 2008 at 18:03
  • 11
    Which is not a problem in 2018. I think this should be higher :-)
    – Michal B.
    Commented Oct 16, 2018 at 7:55
34
USE YourDBName
GO 
SELECT *
FROM sys.Tables
GO

OR

USE YourDBName
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES 
GO
1
  • select * from [databaseA].sys.tables also works, if you need to access table list of a different database same session. Commented Jan 13 at 1:34
11
SELECT * FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'

SQL Server 2012

10
exec sp_msforeachtable 'print ''?'''
10

select * from sysobjects where xtype='U'

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

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

8

Any of the T-SQL code below will work in SQL Server 2019:

-- here, you need to prefix the database name in INFORMATION_SCHEMA.TABLES
SELECT TABLE_NAME FROM [MSSQL-TEST].INFORMATION_SCHEMA.TABLES;

-- The next 2 ways will require you to point
-- to the specific database you want to list the tables

USE [MSSQL-TEST];
-- (1) Using sys.tables
SELECT * FROM sys.tables;

-- (2) Using sysobjects
SELECT * FROM sysobjects
WHERE type='U';
7

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
6

UPDATE 2022: You can list/show the tables that you created with this simple query in Microsoft SQL SERVER.

select * from SYS.TABLES;
2
  • FYI This does not include the table schema in the results. Which was big deal for me, but the old SELECT * FROM INFORMATION_SCHEMA.TABLES does.
    – rob
    Commented Jul 26, 2023 at 13:33
  • WHERE schema_id = <integer> (for me DBO was ID 1)
    – markau
    Commented Nov 28, 2023 at 6:15
5
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U' 
1
  • 1
    SELECT name FROM sysobjects WHERE xtype='U' AND name <> 'sysdiagrams'; because the sysdiagrams table although created by Microsoft SQL Server Management Studio is technically not a system table but one we usually like to exclude anyway.
    – Christoph
    Commented Jun 22, 2015 at 8:42
5

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
3

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.
2
--for oracle
select tablespace_name, table_name from all_tables;

This link can provide much more information on this topic

1
  • 4
    This is not for SQL Server, so is not an answer to this question.
    – Dan Getz
    Commented Jan 15, 2016 at 19:59
2

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
1
SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_TYPE='BASE TABLE' 
ORDER BY TABLE_NAME
1

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 
1

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

1

To remove tables added by replication and any other table Microsoft adds run this:

SELECT s.NAME SchemaName, t.NAME TableName
FROM [dbname].SYS.tables t
INNER JOIN [dbname].SYS.SCHEMAS s
ON t.SCHEMA_ID = s.SCHEMA_ID
WHERE t.is_ms_shipped=0 and type_desc = 'USER_TABLE'
ORDER BY s.NAME, t.NAME
2
  • So, this will work to solve OP's question, with the exception of the WHERE clause? Commented Aug 19, 2021 at 16:17
  • Where clause removes tables microsoft adds and any system replication tables
    – JoelF
    Commented Nov 8, 2021 at 18:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.