SQL SERVER – 2005 List All Tables of Database

This is very simple and can be achieved using system table sys.tables.

USE YourDBName
GO

SELECT
*
FROM sys.Tables
GO

This will return all the tables in the database which user have created.

Reference : Pinal Dave (https://blog.sqlauthority.com) , BOL

SQL Scripts, SQL System Table
Previous Post
SQL SERVER – Explanation and Example Four Part Name
Next Post
SQL SERVER – Running Batch File Using T-SQL – xp_cmdshell bat file

Related Posts

448 Comments. Leave new

  • Hi Pinal,

    i have 400 tables in a database. I have a requirement which need exhaustive document showing all the dependencies and information about all tables in a database(user tables). Please let me know if there is anyway to do this.

    Reply
  • Sourabh Sachdeva
    August 26, 2009 8:02 pm

    you can see all table information by

    select * from sys.tables

    or

    select * from infomation_schema.tables

    or

    sp_tables

    you can see all procedures by:

    select * from sys.objects where type=’p’

    you can see all databases by:

    sp_databases

    you can see all indexes by:

    select * from sys.indexes

    Reply
  • How to get tables from sqldatabase

    Reply
  • Dear sir,
    i want to know how to see the structure of all tables…..

    sp_help tablename gives the output only for 1 table..i have 200 tables in my db.. should i hit the query for each table.. or is there any other solutuion for it..

    plz reply…
    thanx in advance.

    Reply
  • Sourabh Sachdeva
    September 7, 2009 6:41 pm

    Hi Suyog,

    You can see the structure of all tables. by using….

    select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME in(select TABLE_NAME from INFORMATION_SCHEMA.TABLES)

    Reply
  • Hi Sourabh,

    UR answer is ohkk.. but i need INDEX constaints also in it.. so wat should i do??
    plz reply..

    Reply
  • Great man!

    Reply
  • Dear Sir,
    that query is not showing me index ,constraints,columns details…
    is there any other way..except sp_help tablename..

    can we go for sp_msforeachtable???
    can u plz provide exact query for same..

    Reply
  • Sourabh Sachdeva
    September 9, 2009 3:40 pm

    hi suyog,

    If you want to see structure of all tables you can use:

    select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME in(select TABLE_NAME from INFORMATION_SCHEMA.TABLES)

    If you want to see index details then you can use:

    select name,type_desc from sys.objects where type=’PK’

    Reply
  • Sourabh Sachdeva
    September 10, 2009 2:59 pm

    Hi suyog,

    Not a proper way but i think u can use

    declare @tableName as varchar(100)
    declare @countValues as int
    select @tableName=name from sys.tables
    select @countValues=COUNT(*) from sys.tables
    declare @startvalue as int
    set @startvalue=1
    while @startvalue<@countValues
    begin
    declare @exe as varchar(100)
    set @exe='sp_help '+@tableName+''
    print @exe
    exec(@exe)
    set @startvalue=@startvalue+1
    end

    after executing these statements you can see all tables structure info. If you want to combine all data into single resultset you can use temporary tables etc..

    Reply
  • you can use this,
    sp_msforeachtable ‘sp_help [?]’

    Reply
  • Appreciate your blog. Does anyone have any ideas on how to lookup the source table/column associated with a column in a given view? I’m looking for a way to map a column from a view (that shows up in syscolumns) back to the originating table and column that the view column is based on.

    Reply
  • how do i know the structure of table like in mysql or oracle viz desc.i uses sp_privileges but it return the result that i dont want. i want to know the data type and its size

    Reply
  • zee

    Select * from table name
    where Fname like ‘%a%’ and Fname like ‘_li’

    i hope you like it

    Reply
  • zee

    Select RNo+2 As RNo,FName From DBtab
    Where FName=’lia’

    Reply
  • Thanks Sir…
    Big help

    Reply
  • kapil dhariwal
    October 9, 2009 6:46 pm

    select * from sysobjects where xtype=’U’

    Reply
  • How to find the number of records from all the tables in sql 2005?

    Reply
  • Sourabh Sachdeva
    October 31, 2009 6:39 pm

    Hi Lakshmi,

    I think there is no any build in table or any built in function in sql server through you can count number of records in all the tables.

    But i have created a logic through you can get number of records from all tables….

    You can use this:

    declare @dynamicqry as varchar(1000)
    create table temp(tid int)
    declare @startvalue as int
    declare @endvalue as int
    set @startvalue=1
    select @endvalue=count(*) from INFORMATION_SCHEMA.TABLES
    while @startvalue<@endvalue
    begin
    declare @tablename as varchar(100)
    select @tablename=Table_name from (select Table_name,ROW_NUMBER() over(order by table_name asc) 'RowNum' from information_schema.tables) tt where RowNum=@startvalue
    set @dynamicqry='insert temp select count(*) from '+@tablename
    print @dynamicqry
    exec(@dynamicqry)
    set @startvalue+=1
    end
    select SUM(tid) from temp

    Reply
  • Hello,

    I tried to run the following query for display columnames in Ascending order from table Item:-

    exec sp_columns Item order by Column_name Asc

    I get the following error:-
    Msg 156, Level 15, State 1, Line 1
    Incorrect syntax near the keyword ‘order’.

    How shhould I construct the query in order to get the desired results?

    Reply

Leave a Reply