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


当前回答

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

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 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

exec sp_msforeachtable 'print ''?'''

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

select * from SYS.TABLES;
Any of the T-SQL code below will work in SQL Server 2019:

-- here, you need to prefix the database name in INFORMATION_SCHEMA.TABLES
SELECT TABLE_NAME FROM [MSSQL-TEST].INFORMATION_SCHEMA.TABLES;

-- The next 2 ways will require you to point
-- to the specific database you want to list the tables

USE [MSSQL-TEST];
-- (1) Using sys.tables
SELECT * FROM sys.tables;

-- (2) Using sysobjects
SELECT * FROM sysobjects
WHERE type='U';

Here’s a working example using [Skyvia] using sys.tables.

[Skyvia] should be the link to https://skyvia.com/connectors/sql-server


  [1]: https://i.stack.imgur.com/o3qo9.png

Your SQL GUI tool should also have a way to list down all the tables in a database like the one above.

So, whatever suits your need and taste, there’s a code or GUI tool for that.

在SSMS中,要获得特定数据库(例如,“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]

结果:

MyDatabase.dbo.MyTable1 MyDatabase.dbo.MyTable2 MyDatabase.MySchema.MyTable3 MyDatabase.MySchema.MyTable4 等。