declare @maxid int,@minid int =1
DECLARE @IamTemp table( id int identity, table_schema SYSNAME ,table_name SYSNAME)
declare @resultset table(tablename SYSNAME,rowcnt bigint)
INSERT @IamTemp (table_schema,table_name)
SELECT TABLE_SCHEMA,TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
select @maxid = MAX(id) from @IamTemp
WHILE(@minid <= @maxid)
begin
declare @tablename sysname
select @tablename = '['+ TABLE_SCHEMA+'].['+TABLE_NAME+'] ' from @IamTemp where id = @minid
declare @sqlcmd nvarchar(2000) = 'SELECT '''+@tablename+'''as Table_name, COUNT(1) FROM ' + @tablename + ' (NOLOCK)'
INSERT @resultset(tablename,rowcnt )
EXECUTE SP_EXECUTESQL @sqlcmd
SET @minid = @minid + 1
end
SELECT * FROM @resultset ORDER BY rowcnt DESC
DECLARE @IamTemp table( id int identity, table_schema SYSNAME ,table_name SYSNAME)
declare @resultset table(tablename SYSNAME,rowcnt bigint)
INSERT @IamTemp (table_schema,table_name)
SELECT TABLE_SCHEMA,TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
select @maxid = MAX(id) from @IamTemp
WHILE(@minid <= @maxid)
begin
declare @tablename sysname
select @tablename = '['+ TABLE_SCHEMA+'].['+TABLE_NAME+'] ' from @IamTemp where id = @minid
declare @sqlcmd nvarchar(2000) = 'SELECT '''+@tablename+'''as Table_name, COUNT(1) FROM ' + @tablename + ' (NOLOCK)'
INSERT @resultset(tablename,rowcnt )
EXECUTE SP_EXECUTESQL @sqlcmd
SET @minid = @minid + 1
end
SELECT * FROM @resultset ORDER BY rowcnt DESC
No comments:
Post a Comment