Get RowCount from all Tables in DB on RemoteServer

  • info 58414

    Hall of Fame

    Points: 3383

    Hi,
    from my current SQL-Instance,
    i want to get the RowCount from all User-Tables of a  DB on a RemoteServer.
    (LinkedServer or Openquery ??)

    Thanks
    Regards
    Nicole

  • Kenny Jozi

    SSCrazy

    Points: 2004

    You can achieve this using a linked server with the query below, just tweak it to use the linked server .

    NB:  The account used for the linked server should have VIEW DATABASE STATE Permissions on the remote server database

    SELECT
      QUOTENAME(SCHEMA_NAME(sOBJ.schema_id)) + '.' + QUOTENAME(sOBJ.name) AS [TableName]
      , SUM(sPTN.Rows) AS [RowCount]
    FROM
      sys.objects AS sOBJ
      INNER JOIN sys.partitions AS sPTN
        ON sOBJ.object_id = sPTN.object_id
    WHERE
      sOBJ.type = 'U'
      AND sOBJ.is_ms_shipped = 0x0
      AND index_id < 2 -- 0:Heap, 1:Clustered
    GROUP BY
      sOBJ.schema_id
      , sOBJ.name
    ORDER BY [TableName]

    SQL 2000/2005/2008/2012 DBA - MCTS/MCITP

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply