Use sp_MSForEachDB instead of your own loop

sp_msforeachdb 'select ''?'' as DBName, count(*) as ObjectCount from ?.sys.objects'

sp_msforeachtable 'select ''?'', count(*) from?'

sp_msforeachtable 'update statistics ?'

原文地址:https://www.cnblogs.com/aot/p/2917050.html