列表出有数据的表:
select name from sysobjects o where type='u' and exists ( select * from sysindexes where id=o.id and indid<2 and rows>0 )
OR
select o.name from sysobjects o,sysindexes i where o.type='u' and i.id=o.id and i.indid<2 and i.rows>0
列出数据:
declare @tablename varchar(100),@ii varchar(10),@temp nvarchar(1000),@temp2 nvarchar(1000) declare @i int,@count int,@datecount int select @count=count(*) from sysobjects where xtype='u' set @i=1 while(@i<=@count) begin set @ii=@i set @temp='select top 1 @name=name from(select top '+@ii+' name from sysobjects where xtype=''u'' order by name)as a order by name desc' exec sp_executesql @temp,N'@name nvarchar(50) output',@tablename output set @temp2='select @date=count(*) from '+@tablename+'' exec sp_executesql @temp2,N'@date int output',@datecount output if(@datecount>0) begin exec('select * from '+@tablename+'') print @tablename end set @i=@i+1 end