Count all rows from all tables in two databases on different servers

Jonathan Porter

I would like my query to return the table name, and rowcount for all of the tables on our two reporting servers. They both have the same tables. Also, I already added the linked server the other day between these two.

Query so far for one server, not sure how to add a third column connected with our other server though:

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) 

Desired output:

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

This would be a great use for Common Table Expressions (CTE's) as you can run multiple queries, then join those query results together and analyze/manipulate them in different ways:

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

You could also accomplish this with APPLY statements or correlated sub-queries, but the advantage to using a CTE is that you're not running the sub-query for every single row that the parent query returns. Using a CTE you can run a query and then simply treat that query result as if it were a another table.

Obviously you'll want to test this. I don't have access to a SQL Server at the moment, so there may be a typo here or there.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to compare all columns of all tables from two databases

From Dev

Selecting all rows from two tables

From Dev

Get all rows from two tables with SQLite

From Dev

Selecting all records from two different tables

From Dev

Syncing phpMyAdmin databases/tables automatically across two different servers

From Dev

Get a list of all rows from one table, that satisfies multiple criteria in two different tables

From Dev

Search data from two different tables that two tables in different databases

From Dev

Pulling from two databases, three different tables

From Dev

Perl: Syncing two tables from different servers

From Dev

schemacrawler returning tables from all available databases

From Dev

Row Count from all SQL Server Databases

From Dev

Sync two tables from two different databases with different structure MySql

From Dev

Delete rows from all tables

From Dev

SQL. Select all Serial Numbers from two different tables

From Dev

How to Union different tables from two different databases?

From Dev

How to Union different tables from two different databases?

From Dev

Merging two child tables from two different databases

From Dev

Count rows from different tables in join statement

From Dev

Count all rows from a column, and get 2 different count columns on the result even if there are not records to count

From Dev

Fetch all rows of the two joined tables at once

From Dev

I need a query that will return all rows and all columns from two tables when one field is in common

From Dev

how to synchronize two databases of different servers?

From Dev

Get tables size info from all databases from SQL Server

From Dev

Total from the count of two different tables

From Dev

How can I union all rows of two different tables using MySQL 8.0.17 version?

From Dev

Combine rows from two tables with different columns?

From Dev

Search all tables in all databases on server for a string

From Dev

Select all rows from 2 similar tables

From Dev

SQL: Count two different columns from two different tables

Related Related

  1. 1

    How to compare all columns of all tables from two databases

  2. 2

    Selecting all rows from two tables

  3. 3

    Get all rows from two tables with SQLite

  4. 4

    Selecting all records from two different tables

  5. 5

    Syncing phpMyAdmin databases/tables automatically across two different servers

  6. 6

    Get a list of all rows from one table, that satisfies multiple criteria in two different tables

  7. 7

    Search data from two different tables that two tables in different databases

  8. 8

    Pulling from two databases, three different tables

  9. 9

    Perl: Syncing two tables from different servers

  10. 10

    schemacrawler returning tables from all available databases

  11. 11

    Row Count from all SQL Server Databases

  12. 12

    Sync two tables from two different databases with different structure MySql

  13. 13

    Delete rows from all tables

  14. 14

    SQL. Select all Serial Numbers from two different tables

  15. 15

    How to Union different tables from two different databases?

  16. 16

    How to Union different tables from two different databases?

  17. 17

    Merging two child tables from two different databases

  18. 18

    Count rows from different tables in join statement

  19. 19

    Count all rows from a column, and get 2 different count columns on the result even if there are not records to count

  20. 20

    Fetch all rows of the two joined tables at once

  21. 21

    I need a query that will return all rows and all columns from two tables when one field is in common

  22. 22

    how to synchronize two databases of different servers?

  23. 23

    Get tables size info from all databases from SQL Server

  24. 24

    Total from the count of two different tables

  25. 25

    How can I union all rows of two different tables using MySQL 8.0.17 version?

  26. 26

    Combine rows from two tables with different columns?

  27. 27

    Search all tables in all databases on server for a string

  28. 28

    Select all rows from 2 similar tables

  29. 29

    SQL: Count two different columns from two different tables

HotTag

Archive