1

我希望我的查询返回表名,以及我们两个报告服务器上所有表的行数。他们都有相同的表。另外,前几天我已经在这两者之间添加了链接服务器。

到目前为止查询一台服务器,但不确定如何添加与我们的另一台服务器连接的第三列:

SELECT 
    t.NAME AS TableName,
    p.[Rows]
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
WHERE 
    t.NAME NOT LIKE 'dt%' AND
    i.OBJECT_ID > 255 AND   
    i.index_id <= 1
GROUP BY 
    t.NAME, i.object_id, i.index_id, i.name, p.[Rows]
ORDER BY 
    object_name(i.object_id) 

期望的输出:

TableName   DB1_rows     DB2_Rows
----------+-----------+-----------
Account   |  20,000   |  19,388
Contacts  |   1,234   |   1,390
Bla       |   2,330   |   2,430
4

3 回答 3

2

这对于公用表表达式 (CTE) 非常有用,因为您可以运行多个查询,然后将这些查询结果连接在一起并以不同的方式分析/操作它们:

/* Use the WITH keyword to start your first expression */
WITH SERVER_A AS (
  SELECT 
      t.NAME AS TableName,
      p.[Rows] AS NumRows
  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
  WHERE 
      t.NAME NOT LIKE 'dt%' AND
      i.OBJECT_ID > 255 AND   
      i.index_id <= 1
  GROUP BY 
      t.NAME, i.object_id, i.index_id, i.name, p.[Rows]
),

/* Then chain additional expressions (this time adding the linked server into the table name) */
SERVER_B AS (
  SELECT 
      t.NAME AS TableName,
      p.[Rows] AS NumRows
  FROM 
      LINKED_SERVER_NAME.sys.tables t
  INNER JOIN      
      LINKED_SERVER_NAME.sys.indexes i ON t.OBJECT_ID = i.object_id
  INNER JOIN 
      LINKED_SERVER_NAME.sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
  INNER JOIN 
      LINKED_SERVER_NAME.sys.allocation_units a ON p.partition_id = a.container_id
  WHERE 
      t.NAME NOT LIKE 'dt%' AND
      i.OBJECT_ID > 255 AND   
      i.index_id <= 1
  GROUP BY 
      t.NAME, i.object_id, i.index_id, i.name, p.[Rows]
)

/* Then join the two together on a common column */
SELECT
  A.TableName,
  A.NumRows AS DB1_Rows,
  B.NumRows AS DB2_Rows

FROM SERVER_A A
  LEFT JOIN SERVER_B B ON
    A.TableName = B.TableName

ORDER BY
  A.TableName ASC

您也可以使用APPLY语句或相关子查询来完成此操作,但使用 CTE 的优点是您不会为父查询返回的每一行运行子查询。使用 CTE,您可以运行查询,然后简单地将查询结果视为另一个表。

显然你会想要测试这个。我目前无法访问 SQL Server,因此这里或那里可能存在拼写错误。

于 2017-01-06T16:16:26.660 回答
1
DECLARE @RESULT TABLE (TableName VARCHAR(MAX),   DB1_rows  INT,   DB2_Rows INT)
DECLARE @TABLENAME VARCHAR(MAX), @SQL VARCHAR(MAX)

DECLARE cCursor CURSOR FOR 
SELECT name FROM sys.tables

OPEN cCursor 
FETCH NEXT FROM cCursor INTO @TABLENAME 

WHILE @@FETCH_STATUS = 0
    BEGIN 
        SET @SQL = 'SELECT  ''' + @TABLENAME + ''' , COUNT(*) FROM ' + @TABLENAME

        DECLARE @FirstColumn VARCHAR(MAX) = (SELECT TOP 1 c.name FROM sys.columns c JOIN sys.tables t ON t.object_id = c.object_id WHERE t.name = @TABLENAME ORDER BY column_id)

        SET @SQL = 'SELECT  ''' + @TABLENAME + ''' , SUM(CASE WHEN A.' + @FirstColumn + ' IS NOT NULL THEN 1 ELSE 0 END), SUM(CASE WHEN B.' + @FirstColumn + ' IS NOT NULL THEN 1 ELSE 0 END) '
                   +'FROM LIVE.dbo.' + @TABLENAME + ' AS A FULL JOIN TEST.dbo.' + @TABLENAME + ' AS B on 1=0'

        INSERT INTO @RESULT EXEC (@SQL) 

        FETCH NEXT FROM cCursor INTO @TABLENAME 
    END

CLOSE cCURSOR 
DEALLOCATE cCURSOR

SELECT * FROM @RESULT

只需将“SET @SQL”语句第二行的 LIVE 和 TEST 以及“dbo”模式名称更改为 2 个数据库的名称。

编辑:您还可以将数据库名称之一添加到顶部的“SELECT name FROM sys.tables”语句中,以及您想要执行的任何表名过滤。

于 2017-01-06T16:05:18.053 回答
0

如果您能够从一台服务器获得结果,那么如果已设置链接服务器,您可以从另一台服务器获得相同的结果。

您可以使用 4 部分名称来执行此操作。例如:

Select ...
From ServerName.DBName.schema.TableName
...
于 2017-01-06T15:40:39.963 回答