首页 > 解决方案 > 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

标签: sql

解决方案


它的工作原理如下:

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

推荐阅读