由於在工作中經常需要檢查數據庫中的表有沒有數據,所以寫了下面的代碼,以方便檢查空表。
declare @juzi varchar(max)
declare @temp varchar(max)
set @juzi = 'select n,c from (select ''n'' n, 0 c'
DECLARE MyCursor CURSOR FOR
select 'select '''+b.name+'.'+a.name+''' n,count(*) c from '+b.name+'.'+a.name from sys.objects a
inner join sys.schemas b on a.schema_id=b.schema_id
where type='U'
open MyCursor
FETCH NEXT FROM MyCursor
INTO @temp
while @@fetch_status = 0
begin
set @juzi = @juzi + ' union ' + @temp + ''
fetch next from MyCursor into @temp
end
close MyCursor
deallocate MyCursor
set @juzi = @juzi + ') t'
select @juzi