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


当前回答

2022年更新: 您可以在Microsoft SQL SERVER中列出/显示使用这个简单查询创建的表。

select * from SYS.TABLES;

其他回答

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 SYSSCHEMA.NAME, SYSTABLE.NAME
FROM SYS.tables SYSTABLE
INNER JOIN SYS.SCHEMAS SYSSCHEMA
ON SYSTABLE.SCHEMA_ID = SYSSCHEMA.SCHEMA_ID
SELECT * FROM INFORMATION_SCHEMA.TABLES 

OR

SELECT * FROM Sys.Tables

感谢Ray Vega,他的响应给出了数据库中的所有用户表…

Exec sp_msforeachtable 'print " ?“‘

Sp_helptext显示了底层查询,它总结为…

select * from dbo.sysobjects o 
join sys.all_objects syso on o.id =  syso.object_id  
where OBJECTPROPERTY(o.id, 'IsUserTable') = 1 
and o.category & 2 = 0 
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'