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


当前回答

select * from sysobjects where xtype='U'

其他回答

exec sp_msforeachtable 'print ''?'''
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'

下面是你可以搜索的其他对象类型的列表:

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

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

select * from SYS.TABLES;
SELECT * FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'

SQL Server 2012

select * from sysobjects where xtype='U'