How do I get list of all tables in a database using TSQL?

前端 未结 17 1787
无人及你
无人及你 2020-11-22 14:37

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

相关标签:
17条回答
  • 2020-11-22 15:06
    SELECT sobjects.name
    FROM sysobjects sobjects
    WHERE sobjects.xtype = 'U' 
    
    0 讨论(0)
  • 2020-11-22 15:09

    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' 
    
    0 讨论(0)
  • 2020-11-22 15:10

    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
    
    0 讨论(0)
  • 2020-11-22 15:11

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

    0 讨论(0)
  • 2020-11-22 15:13

    select * from sysobjects where xtype='U'

    0 讨论(0)
  • 2020-11-22 15:16
    SELECT name 
    FROM sysobjects 
    WHERE xtype='U' 
    ORDER BY name;
    

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

    0 讨论(0)
提交回复
热议问题