列出 SQL Server 2012 架构中所有表的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21312331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:51:37  来源:igfitidea点击:

List names of all tables in a SQL Server 2012 schema

sqlsql-server

提问by AnkitGarg43

I have a schema in SQL Server 2012.

我在 SQL Server 2012 中有一个架构。

Is there a command that I can run in SQL to get the names of all the tables in that schema that were populated by user?

是否有可以在 SQL 中运行的命令来获取该模式中由用户填充的所有表的名称?

I know a similar query for MySQL SHOW TABLES;but this does not work with SQL Server.

我知道一个类似的 MySQL 查询,SHOW TABLES;但这不适用于 SQL Server。

回答by Kev

Your should really use the INFORMATION_SCHEMAviews in your database:

您应该真正使用INFORMATION_SCHEMA数据库中的视图:

USE <your_database_name>
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES

You can then filter that by table schema and/or table type, e.g.

然后,您可以按表架构和/或表类型对其进行过滤,例如

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

回答by Aaron Bertrand

SELECT t.name 
  FROM sys.tables AS t
  INNER JOIN sys.schemas AS s
  ON t.[schema_id] = s.[schema_id]
  WHERE s.name = N'schema_name';

回答by Nandoviski

SQL Server 2005, 2008, 2012 or 2014:

SQL Server 2005、2008、2012 或 2014:

SELECT * FROM information_schema.tables WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA = 'dbo'

For more details: How do I get list of all tables in a database using TSQL?

有关更多详细信息: 如何使用 TSQL 获取数据库中所有表的列表?

回答by Lorena Pita

SELECT t1.name AS [Schema], t2.name AS [Table]
FROM sys.schemas t1
INNER JOIN sys.tables t2
ON t2.schema_id = t1.schema_id
ORDER BY t1.name,t2.name

回答by jtimperley

SELECT *
FROM sys.tables t
INNER JOIN sys.objects o on o.object_id = t.object_id
WHERE o.is_ms_shipped = 0;

回答by Saman Kapali

select * from [schema_name].sys.tables

This should work. Make sure you are on the server which consists of your "[schema_name]"

这应该有效。确保您在由您的服务器组成的服务器上"[schema_name]"