Monday, July 10, 2017

SQL Server -Query for Finding count of rows in each table in db


declare @RowCount int
declare @MaxRows int
declare @ExecuteSql varchar(1000)
declare @Table varchar(100)
declare @No int

set @RowCount=1

create table #Temp_table  (ID int, TABLE_NAME varchar(100),Row int)

insert into #Temp_table(ID,TABLE_NAME)
select ROW_NUMBER() OVER(ORDER BY TABLE_NAME) AS ROW ,TABLE_SCHEMA+'.'+ TABLE_NAME  from INFORMATION_SCHEMA.TABLES

select @MaxRows=MAX(ID) from #Temp_table

declare @RowofTable table (Row int)

while @RowCount<=@MaxRows
begin
select @Table=TABLE_NAME from #Temp_table where ID=@RowCount

select @ExecuteSql='select count(*) cnt from '+@Table
--+' where cast(As_of as date)=cast(GETDATE() as date)'

insert into @RowofTable(Row)
exec  (@ExecuteSql)

select @No=ROW from @RowofTable
update #Temp_table set Row=@No where ID=@RowCount

select @RowCount=@RowCount+1
delete from @RowofTable
end
select * from #Temp_table
drop table #Temp_table

Thats it !!!!!!!!! Happy Coding!!!!!!!!!!!!!!!!!!!

No comments:

Search This Blog

DAX - Grouping on multiple columns and the count

Please go thorugh the below URL for entire soultion. http://community.powerbi.com/t5/Desktop/DAX-Grouping-on-multiple-columns-and-the-cou...