Tuesday, October 12, 2010

Get list names of tables in database sql server 2008

I was trying to clean up my database and first step stumped me. I couldnt figure out how to get the table names from Sql Server 2008 database


SELECT TABLE_NAME, *
FROM INFORMATION_SCHEMA.TABLES


This works perfectly


Gave me the following information

TABLE_NAMETABLE_CATALOGTABLE_SCHEMATABLE_NAMETABLE_TYPE
categoryDBNamedbocategoryBASE TABLE

Then, I tried the following


SELECT s.name + '.' + o.name as tableName
FROM   sys.schemas s
JOIN   sys.objects o ON s.schema_id = o.schema_id
WHERE  o.type = 'U'
ORDER  BY s.name, o.name

and it gave me the following

TABLE_NAME
dbo.category

Other possible types that can be used:

  • 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

No comments: