导航

查询 SQL_Server 所有表的记录数: for xml path

Posted on 2017-12-01 09:47  DotNet1010  阅读(166)  评论(0编辑  收藏  举报

--我加了 top 10 用的时候可以去掉

declare @select_alltableCount varchar(max)=''
;with T as (
select top 10 'SELECT '''+name+ '''as Table_Name, COUNT(1) as Row_Count From '+ name + char(10)+ ' union all ' as A  from sys.objects where type='U' order by name asc
)
,T2 as (select val=(select  ''+ A +'' from T for xml Path(''))  )
select @select_alltableCount=val from T2
if(Len(@select_alltableCount)>9) SET @select_alltableCount=Left(@select_alltableCount,Len(@select_alltableCount)-9)
print @select_alltableCount
Execute( @select_alltableCount)