Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

How to get row count(*) for each table that matches a pattern

$
0
0

I have the following query that returns all tables that match a pattern (tablename_ and then 4 digits). I also want to return the row counts for these tables.

Currently a single column is returned: tablename. I want to add the column RowCount.

DECLARE @SQLCommand nvarchar(4000)
DECLARE @TableName varchar(128)
SET @TableName = 'ods_TTstat_master'  --<<<<<< change this to a table name
SET @SQLCommand = 'SELECT [name] as zhistTables FROM dbo.sysobjects WHERE name like ''%' + @TableName + '%'' and objectproperty(id,N''IsUserTable'')=1 ORDER BY name DESC'
EXEC sp_executesql @SQLCommand 


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>