sql - SQL多表到达
问题描述
我在这里有这段代码,事情是它查询选择的“TestLog”并显示:
- 姓名,
- 架构,
- 行数,
- 整体空间,
- 已用空间,....
事情是我需要为不止一张桌子查询这些信息,比如说 5 个特定的……有人能帮忙吗?
USE myDB
GO
SELECT
t.name AS TableName,
s.name AS SchemaName,
p.rows AS RowCounts,
SUM(a.total_pages) * 8 / 1024 AS TotalSpaceMB,
SUM(a.used_pages) * 8 / 1024 AS UsedSpaceMB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 / 1024 AS UnusedSpaceMB
FROM
sys.tables t
INNER JOIN sys.indexes i ON t.object_id = i.object_id
INNER JOIN sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id
INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.name = ('TestLog')
AND t.is_ms_shipped = 0
AND i.object_id > 255
GROUP BY
t.name, s.name, p.rows
ORDER BY
t.name;
GO
解决方案
它的工作原理如下:
-- specific tables
WHERE
t.name in ('Test1' , 'Test2' , 'Test3')
AND t.is_ms_shipped = 0
AND i.object_id > 255
-- one specific table
WHERE
t.name = 'TestLog'
AND t.is_ms_shipped = 0
AND i.object_id > 255
-- specific tables with test in front
WHERE
t.name like 'Test%'
AND t.is_ms_shipped = 0
AND i.object_id > 255
推荐阅读
- r - Leaflet AddCircleMarkers 设置为取消选择,直到用户选择 - R SHINY
- android - Robolectric,如何断言活动已创建并且没有异常
- c - makefile 多重定义错误
- vb.net - 如何在 VB.net 中进行三元语句
- c++ - 用OpenGL画一条任意线(即轴范围没有限制)
- android - 使用 RxJava 进行链式改造调用授权和后续调用
- performance - 我在 4 小时内向 Solr7.4 插入大量数据,Solr 查询变得非常慢
- html - 列表组的垂直自动滚动
- excel - 导出列表并将其保存为 .CSV 文件
- firebase - 如何从 Firebase 的 BigQuery 中导出 csv 数据(包括记录类型)