在 SQL Server 上获取特定数据库中所有表的名称的最佳方法是什么?
19 回答
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'
或者,
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 sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
以下是您也可以搜索的其他对象类型的列表:
- AF:聚合函数 (CLR)
- C:检查约束
- D:默认或 DEFAULT 约束
- F:外键约束
- L:日志
- FN:标量函数
- FS:汇编(CLR)标量函数
- FT:汇编 (CLR) 表值函数
- IF:内联表函数
- IT:内部表
- P:存储过程
- PC:装配 (CLR) 存储过程
- PK:PRIMARY KEY 约束(类型为 K)
- RF:复制过滤器存储过程
- S:系统表
- SN:同义词
- SQ:服务队列
- TA:装配 (CLR) DML 触发器
- TF:表函数
- TR:SQL DML 触发器
- TT:表类型
- U:用户表
- UQ:UNIQUE 约束(类型为 K)
- 五:查看
- X:扩展存储过程
SELECT * FROM INFORMATION_SCHEMA.TABLES
或者
SELECT * FROM Sys.Tables
USE YourDBName
GO
SELECT *
FROM sys.Tables
GO
或者
USE YourDBName
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES
GO
SELECT * FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'
SQL Server 2012
exec sp_msforeachtable 'print ''?'''
select * from sysobjects where xtype='U'
SELECT name
FROM sysobjects
WHERE xtype='U'
ORDER BY name;
(SQL Server 2000 标准;在 SQL Server 2005 中仍受支持。)
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
缺点INFORMATION_SCHEMA.TABLES
是它还包括系统表,例如dtproperties
和MSpeer_...
表,无法将它们与您自己的表区分开来。
我建议使用(已弃用的sysobjectssys.objects
视图的新版本 ),它确实支持排除系统表:
select *
from sys.objects
where type = 'U' -- User tables
and is_ms_shipped = 0 -- Exclude system tables
那么您可以使用sys.objects来获取所有数据库对象。
GO
select * from sys.objects where type_desc='USER_TABLE' order by name
GO
或者
-- 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
在 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
- 等等
请使用这个。您将获得表名和模式名:
SELECT SYSSCHEMA.NAME, SYSTABLE.NAME
FROM SYS.tables SYSTABLE
INNER JOIN SYS.SCHEMAS SYSSCHEMA
ON SYSTABLE.SCHEMA_ID = SYSSCHEMA.SCHEMA_ID
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.
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
ORDER BY TABLE_NAME
感谢 Ray Vega,他的回复提供了数据库中的所有用户表......
exec sp_msforeachtable '打印''?'''
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 * FROM INFORMATION_SCHEMA.COLUMNS
还会向您显示所有表和相关列。
要删除复制添加的表和 Microsoft 添加的任何其他表,请运行以下命令:
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