views:

4352

answers:

9

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

+2  A: 
exec sp_msforeachtable 'print ''?'''
Ray Vega
+1  A: 

select * from sysobjects where xtype='U'

spoulson
+1  A: 

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

(sql2000 standard, still supported in 2005)

devio
A: 
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
Erikk Ross
+7  A: 

SELECT * FROM INFORMATION_SCHEMA.TABLES

or Sys.Tables

...all these sysobjects queries, and I thought I was old school =)

StingyJack
This is the most database-agnostic way to do it :)
ranomore
Just a note that (as mentioned in other answers) sys.tables is only available in 2005 onwards
Rob
+11  A: 

SQL Server 2005 or 2008:

SELECT * FROM information_schema.tables

SQL Server 2000:

SELECT * FROM sysobjects WHERE xtype='U'
ScottStonehouse
+5  A: 
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'

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

  • C: Check constraint
  • D: Default constraint
  • F: Foreign Key constraint
  • L: Log
  • P: Stored procedure
  • PK: Primary Key constraint
  • RF: Replication Filter stored procedure
  • S: System table
  • TR: Trigger
  • U: User table
  • UQ: Unique constraint
  • V: View
  • X: Extended stored procedure
Micah
Very nice answer.
DiGi