在SQL Server上获得特定数据库中所有表的名称的最佳方法是什么?
当前回答
你可以用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
其他回答
感谢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
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.
INFORMATION_SCHEMA的缺点。它还包括系统表,如dtproperties和MSpeer_…表,没有办法把它们和你自己的表区分开来。
我建议使用sys。对象(已弃用的sysobjects视图的新版本),它支持排除系统表:
select *
from sys.objects
where type = 'U' -- User tables
and is_ms_shipped = 0 -- Exclude system tables
你可以用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 * FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'
SQL Server 2012
推荐文章
- SQL Server数据库备份恢复到低版本
- SQL Server:过滤sp_who2的输出
- 在SQL Server上使用varchar(MAX) vs TEXT
- Visual Studio: ContextSwitchDeadlock
- 模式、表和数据库之间的区别是什么?
- Sql Server字符串到日期的转换
- 如何将SQL Azure数据库复制到本地开发服务器?
- SQL Server 2008不能用新创建的用户登录
- SQL Server中User和Login的区别
- 在参数声明中,varchar(MAX)的大小是多少?
- 数据库性能调优有哪些资源?
- 为两列的组合添加唯一的约束
- SQL Server的隐藏特性
- 方括号[]在sql语句中有什么用?
- 对象'DF__*'依赖于列'*' -将int改为double