Reputation: 192216
What is the best way to get the names of all of the tables in a specific database on SQL Server?
Upvotes: 1119
Views: 2296577
Reputation: 95
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA ='yourdbname'
Note: replace the 'yourdbname' with your database name.
Example:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA ='mydb'
Upvotes: -1
Reputation: 8741
On SQL Server Express 2022, using ANSI standard view INFORMATION_SCHEMA.TABLES, we can get user tables only by
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND TABLE_NAME NOT IN ('dtproperties', 'sysdiagrams');
This excludes 2 system tables dbo.dtproperties and dbo.sysdiagrams shown on SSMS, System Tables Folder :
Those System Tables are not to be modified directly by user.
Upvotes: 1
Reputation: 199
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';
Upvotes: 8
Reputation: 884
UPDATE 2022: You can list/show the tables that you created with this simple query in Microsoft SQL SERVER.
select * from SYS.TABLES;
Upvotes: 6
Reputation: 37
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
Upvotes: 1
Reputation: 24965
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'
Upvotes: 1726
Reputation: 19469
SELECT * FROM INFORMATION_SCHEMA.TABLES
OR
SELECT * FROM Sys.Tables
Upvotes: 123
Reputation: 3833
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
Upvotes: 5
Reputation: 814
USE YourDBName
GO
SELECT *
FROM sys.Tables
GO
OR
USE YourDBName
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES
GO
Upvotes: 34
Reputation: 3964
Using SELECT * FROM INFORMATION_SCHEMA.COLUMNS
also shows you all tables and related columns.
Upvotes: 1
Reputation: 51
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
Upvotes: 2
Reputation: 499
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:
Upvotes: 3
Reputation: 4372
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
Upvotes: 7
Reputation: 91
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
Upvotes: 1
Reputation: 37633
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
ORDER BY TABLE_NAME
Upvotes: 1
Reputation: 296
--for oracle
select tablespace_name, table_name from all_tables;
This link can provide much more information on this topic
Upvotes: 2
Reputation: 116090
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
Here is a list of other object types you can search for as well:
Upvotes: 216
Reputation: 2715
SELECT * FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'
SQL Server 2012
Upvotes: 11
Reputation: 37215
SELECT name
FROM sysobjects
WHERE xtype='U'
ORDER BY name;
(SQL Server 2000 standard; still supported in SQL Server 2005.)
Upvotes: 10
Reputation: 2183
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
Upvotes: 5