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

前端 未结 17 1763
无人及你
无人及你 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:18

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

    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:

    • MyDatabase.dbo.MyTable1
    • MyDatabase.dbo.MyTable2
    • MyDatabase.MySchema.MyTable3
    • MyDatabase.MySchema.MyTable4
    • etc.
    0 讨论(0)
  • 2020-11-22 15:19
    SELECT TABLE_NAME 
    FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_TYPE='BASE TABLE' 
    ORDER BY TABLE_NAME
    
    0 讨论(0)
  • 2020-11-22 15:19

    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
    
    0 讨论(0)
  • 2020-11-22 15:20
    SELECT sobjects.name
    FROM sysobjects sobjects
    WHERE sobjects.xtype = 'U'
    

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

    • AF: Aggregate function (CLR)
    • C: CHECK constraint
    • D: Default or DEFAULT constraint
    • F: FOREIGN KEY constraint
    • L: Log
    • FN: Scalar function
    • FS: Assembly (CLR) scalar-function
    • FT: Assembly (CLR) table-valued function
    • IF: In-lined table-function
    • IT: Internal table
    • P: Stored procedure
    • PC: Assembly (CLR) stored-procedure
    • PK: PRIMARY KEY constraint (type is K)
    • RF: Replication filter stored procedure
    • S: System table
    • SN: Synonym
    • SQ: Service queue
    • TA: Assembly (CLR) DML trigger
    • TF: Table function
    • TR: SQL DML Trigger
    • TT: Table type
    • U: User table
    • UQ: UNIQUE constraint (type is K)
    • V: View
    • X: Extended stored procedure
    0 讨论(0)
  • 2020-11-22 15:21
    SELECT * FROM information_schema.tables
    where TABLE_TYPE = 'BASE TABLE'
    

    SQL Server 2012

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