在SQL Server上获得特定数据库中所有表的名称的最佳方法是什么?
SELECT name
FROM sysobjects
WHERE xtype='U'
ORDER BY name;
(SQL Server 2000标准;SQL Server 2005仍然支持。)
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 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
SELECT * FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'
SQL Server 2012
USE YourDBName
GO
SELECT *
FROM sys.Tables
GO
OR
USE YourDBName
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES
GO
--for oracle
select tablespace_name, table_name from all_tables;
这个链接可以提供更多的信息 主题
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
ORDER BY TABLE_NAME
感谢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
INFORMATION_SCHEMA的缺点。它还包括系统表,如dtproperties和MSpeer_…表,没有办法把它们和你自己的表区分开来。
我建议使用sys。对象(已弃用的sysobjects视图的新版本),它支持排除系统表:
select *
from sys.objects
where type = 'U' -- User tables
and is_ms_shipped = 0 -- Exclude system tables
在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
你可以用sys。对象来获取所有数据库对象。
GO
select * from sys.objects where type_desc='USER_TABLE' order by name
GO
OR
-- 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
要删除复制添加的表和微软添加的任何其他表,请运行以下命令:
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
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.
推荐文章
- 在SQL Server中查找重复的行
- 执行带有Output参数的存储过程?
- 从DateTime中提取小时(SQL Server 2005)
- 我如何在T-SQL用逗号格式化一个数字?
- LEFT OUTER JOIN如何返回比左表中存在的记录更多的记录?
- 如何用SQL语句计算百分比
- SQL Server动态PIVOT查询?
- 如何等待2秒?
- SQL Server: CROSS JOIN和FULL OUTER JOIN的区别是什么?
- varchar和nvarchar SQL Server数据类型之间的主要性能差异是什么?
- 向现有表添加主键
- 我应该在SQL varchar(长度)中考虑电话的最长的全球电话号码是什么
- T-SQL CASE子句:如何指定WHEN NULL
- 表中标识列的显式值只能在使用列列表且IDENTITY_INSERT为ON SQL Server时指定
- 如何确定已安装的SQL Server实例及其版本?