在SQL Server上获得特定数据库中所有表的名称的最佳方法是什么?


当前回答

SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U' 

其他回答

select * from sysobjects where xtype='U'

使用SELECT * FROM INFORMATION_SCHEMA。COLUMNS还显示所有表和相关列。

SQL Server 2000, 2005, 2008, 2012, 2014, 2016, 2017或2019:

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'

仅显示来自特定数据库的表

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: SQL Server 2000:

SELECT * FROM sysobjects WHERE xtype='U' 

要删除复制添加的表和微软添加的任何其他表,请运行以下命令:

SELECT s.NAME SchemaName, t.NAME TableName
FROM [dbname].SYS.tables t
INNER JOIN [dbname].SYS.SCHEMAS s
ON t.SCHEMA_ID = s.SCHEMA_ID
WHERE t.is_ms_shipped=0 and type_desc = 'USER_TABLE'
ORDER BY s.NAME, t.NAME
SELECT name 
FROM sysobjects 
WHERE xtype='U' 
ORDER BY name;

(SQL Server 2000标准;SQL Server 2005仍然支持。)