由于在工作中经常需要检查数据库中的表有没有数据,所以写了下面的代码,以方便检查空表。
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
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