日期:2014-05-17 浏览次数:20517 次
use test
go
select name into #tab from sysobjects where xtype='U'--得到所有表名
use test
go
select count(1) from sysobjects where xtype='U'--得到表的数量
if exists (select * from 表名 )--判断表里是否有数据
IF OBJECT_ID('tempdb.dbo.#TB') IS NOT NULL
DROP TABLE #TB
GO
create table #TB([name] VARCHAR(500))
DECLARE @Name VARCHAR(500),@SQL VARCHAR(MAX)
DECLARE NAME CURSOR
FOR
SELECT name FROM sysobjects where xtype='U'
OPEN NAME
FETCH NEXT FROM NAME INTO @Name
WHILE @@FETCH_STATUS=0
BEGIN
SET @SQL='
INSERT INTO #tb SELECT name FROM (select name,COUNT(1) AS num from syscolumns where id=object_id('+@NAME+') GROUP BY name HAVING COUNT(1)>0)t'
EXEC (@sql)
FETCH NEXT FROM NAME INTO @NAME
END
CLOSE NAME
DEALLOCATE NAME
GO
--SELECT * FROM #TB
select t.name
from sys.tables t
inner join sys.partitions p
on t.object_id = p.object_id
where p.rows >0 --数据行数大于0
select distinct o.name
from sysobjects o
inner join sysindexes i
on o.id = i.id
where xtype = 'U'
and i.rows > 0 --数据行数大于0的
select a.name 'TableName',b.rowcnt
from sys.tables a
inner join sys.sysindexes b on a.object_id=b.id and b.indid<=1
where b.rowcnt>0