I have table that contains Database, Schema, and Table Name. I would like to count the row for each rows using group by for defined column in Same Table.
DECLARE @WeeklyRowCount TABLE
(
db VARCHAR(50)
,sch VARCHAR(15)
,Tb VARCHAR(100)
,col VARCHAR(50)
)
DECLARE @cmd VARCHAR(MAX) = (
SELECT ' UNION ALL SELECT COUNT(*) AS C, DATE_STAMP,'''
+ QUOTENAME(sch) + '.' + QUOTENAME(tb)
+ ''' AS T FROM ' + QUOTENAME(db) + '.' + QUOTENAME(sch) + '.' + QUOTENAME(tb) + 'GROUP BY col'
FROM @WeeklyRowCount )
SELECT COUNT(*) AS C, col ,'Table name' AS T FROM db.sch.tb GROUP BY col
I have tried to use dynamic SQL but due to limitation of 8000 character, full query is not showing up.
EXEC (@cmd)does not seem to have the 8000 char limitation. learn.microsoft.com/en-us/sql/t-sql/language-elements/… No need toPRINTthe generated statement