在SQL Server上获得特定数据库中所有表的名称的最佳方法是什么?
当前回答
USE YourDBName
GO
SELECT *
FROM sys.Tables
GO
OR
USE YourDBName
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES
GO
其他回答
SELECT * FROM INFORMATION_SCHEMA.TABLES
OR
SELECT * FROM Sys.Tables
INFORMATION_SCHEMA的缺点。它还包括系统表,如dtproperties和MSpeer_…表,没有办法把它们和你自己的表区分开来。
我建议使用sys。对象(已弃用的sysobjects视图的新版本),它支持排除系统表:
select *
from sys.objects
where type = 'U' -- User tables
and is_ms_shipped = 0 -- Exclude system 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.
USE YourDBName
GO
SELECT *
FROM sys.Tables
GO
OR
USE YourDBName
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES
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
推荐文章
- 选项(RECOMPILE)总是更快;为什么?
- 设置数据库从单用户模式到多用户
- 我如何转义一个百分比符号在T-SQL?
- SQL Server恢复错误-拒绝访问
- 如何在SQL Server Management Studio中查看查询历史
- SQL Server索引命名约定
- 可以为公共表表达式创建嵌套WITH子句吗?
- SQL Server的NOW()?
- 在SQL Server中截断(不是四舍五入)小数位
- 如何在SQL Server数据库中更改列数据类型而不丢失数据?
- 保持简单,以及如何在一个查询中执行多个CTE
- 如何使用TSQL截断数据库中的所有表?
- 如何从表中导出所有数据到可插入的sql格式?
- 我如何执行插入和返回插入的身份与Dapper?
- 如何将时间按小时或10分钟分组?