如何使用 TSQL 获取数据库中所有表的列表?

发布于 2024-07-06 09:51:05 字数 43 浏览 8 评论 0原文

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

What is the best way to get the names of all of the tables in a specific database on SQL Server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

任性一次 2024-07-13 09:51:05

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' 

SQL Server 2000, 2005, 2008, 2012, 2014, 2016, 2017 or 2019:

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'

To show only tables from a particular database

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: For SQL Server 2000:

SELECT * FROM sysobjects WHERE xtype='U' 
慵挽 2024-07-13 09:51:05
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'

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

  • AF:聚合函数 (CLR)
  • C:CHECK 约束
  • D:默认或 DEFAULT 约束
  • F:FOREIGN KEY 约束
  • L:日志
  • FN:标量函数
  • FS:程序集 (CLR)标量函数
  • FT:汇编 (CLR) 表值函数
  • IF:内联表函数
  • IT:内部表
  • P:存储过程
  • PC:汇编 (CLR) 存储过程
  • PK:主键约束(类型为 K)
  • RF:复制过滤存储过程
  • S:系统表
  • SN:同义词
  • SQ:服务队列
  • TA:程序集(CLR)DML 触发器
  • TF:表函数
  • TR:SQL DML 触发器
  • TT:表类型
  • U:用户表
  • UQ:UNIQUE 约束(类型为 K)
  • V :查看
  • X:扩展存储过程
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'

Here is a list of other object types you can search for as well:

  • 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
他夏了夏天 2024-07-13 09:51:05
SELECT * FROM INFORMATION_SCHEMA.TABLES 

或者

SELECT * FROM Sys.Tables
SELECT * FROM INFORMATION_SCHEMA.TABLES 

OR

SELECT * FROM Sys.Tables
失而复得 2024-07-13 09:51:05
USE YourDBName
GO 
SELECT *
FROM sys.Tables
GO

或者

USE YourDBName
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES 
GO
USE YourDBName
GO 
SELECT *
FROM sys.Tables
GO

OR

USE YourDBName
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES 
GO
焚却相思 2024-07-13 09:51:05
SELECT * FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'

SQL Server 2012

SELECT * FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'

SQL Server 2012

冧九 2024-07-13 09:51:05
exec sp_msforeachtable 'print ''?'''
exec sp_msforeachtable 'print ''?'''
吻安 2024-07-13 09:51:05

从 sysobjects 中选择 *,其中 xtype='U'

select * from sysobjects where xtype='U'

心安伴我暖 2024-07-13 09:51:05
SELECT name 
FROM sysobjects 
WHERE xtype='U' 
ORDER BY name;

(SQL Server 2000 标准;SQL Server 2005 中仍受支持。)

SELECT name 
FROM sysobjects 
WHERE xtype='U' 
ORDER BY name;

(SQL Server 2000 standard; still supported in SQL Server 2005.)

思念满溢 2024-07-13 09:51:05

下面的任何 T-SQL 代码都可以在 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';

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';
风吹雪碎 2024-07-13 09:51:05

INFORMATION_SCHEMA.TABLES 的缺点是它还包含系统表,例如 dtpropertiesMSpeer_... 表,但无法区分除了你自己的桌子之外。

我建议使用 < code>sys.objects (已弃用的新版本 sysobjects 视图),它支持排除系统表:

select *
from sys.objects
where type = 'U'      -- User tables
and is_ms_shipped = 0 -- Exclude system tables

The downside of INFORMATION_SCHEMA.TABLES is that it also includes system tables such as dtproperties and the MSpeer_... tables, with no way to tell them apart from your own tables.

I would recommend using sys.objects (the new version of the deprecated sysobjects view), which does support excluding the system tables:

select *
from sys.objects
where type = 'U'      -- User tables
and is_ms_shipped = 0 -- Exclude system tables
伴我心暖 2024-07-13 09:51:05

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

select * from SYS.TABLES;

UPDATE 2022:
You can list/show the tables that you created with this simple query in Microsoft SQL SERVER.

select * from SYS.TABLES;
北城挽邺 2024-07-13 09:51:05
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U' 
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U' 
烟燃烟灭 2024-07-13 09:51:05

那么您可以使用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

Well you can use sys.objects to get all database objects.

 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
不念旧人 2024-07-13 09:51:05

在 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
  • 等。

In SSMS, to get all fully qualified table names in a specific database (E.g., "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]

Results:

  • MyDatabase.dbo.MyTable1
  • MyDatabase.dbo.MyTable2
  • MyDatabase.MySchema.MyTable3
  • MyDatabase.MySchema.MyTable4
  • etc.
阳光的暖冬 2024-07-13 09:51:05
--for oracle
select tablespace_name, table_name from all_tables;

此链接可以提供更多有关此的信息
主题

--for oracle
select tablespace_name, table_name from all_tables;

This link can provide much more information on this
topic

三岁铭 2024-07-13 09:51:05

请使用这个。 您将获得表名称和模式名称:

SELECT SYSSCHEMA.NAME, SYSTABLE.NAME
FROM SYS.tables SYSTABLE
INNER JOIN SYS.SCHEMAS SYSSCHEMA
ON SYSTABLE.SCHEMA_ID = SYSSCHEMA.SCHEMA_ID

Please use this. You will get table names along with schema names:

SELECT SYSSCHEMA.NAME, SYSTABLE.NAME
FROM SYS.tables SYSTABLE
INNER JOIN SYS.SCHEMAS SYSSCHEMA
ON SYSTABLE.SCHEMA_ID = SYSSCHEMA.SCHEMA_ID
玩物 2024-07-13 09:51:05
SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_TYPE='BASE TABLE' 
ORDER BY TABLE_NAME
SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_TYPE='BASE TABLE' 
ORDER BY TABLE_NAME
℡Ms空城旧梦 2024-07-13 09:51:05

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

Thanks to Ray Vega, whose response gives all user tables in a database...

exec sp_msforeachtable 'print ''?'''

sp_helptext shows the underlying query, which summarises to...

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 
自我难过 2024-07-13 09:51:05

使用 SELECT * FROM INFORMATION_SCHEMA.COLUMNS 还会显示所有表和相关列。

Using SELECT * FROM INFORMATION_SCHEMA.COLUMNS also shows you all tables and related columns.

夜深人未静 2024-07-13 09:51:05

要删除通过复制添加的表以及 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

To remove tables added by replication and any other table Microsoft adds run this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文