有什么方法可以获取所有表,条件是如果表不为空(如果存在某些记录)。
我可以使用:
获取表列表USE 'DatabaseName'
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE table_Schema = 'dbo'
ORDER BY table_NAME.
但是,我想要类似
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE table_Schema = 'dbo'
AND "**table has some records present**" ORDER BY table_NAME
答案 0 :(得分:1)
尝试:
SELECT
OBJECT_NAME(T.OBJECT_ID) AS TABLE_NAME,
SUM(P.ROWS) AS TOTAL_ROWS
FROM
sys.tables T
INNER JOIN
sys.partitions P
ON T.OBJECT_ID = P.OBJECT_ID
WHERE
P.INDEX_ID IN (0,1)
GROUP BY
T.OBJECT_ID
HAVING
SUM(P.ROWS) > 0
答案 1 :(得分:0)
运行此
select 'select count(*) [entries],'''+ TABLE_NAME + ''' from '+ quotename(TABLE_SCHEMA) + '.' + quotename(TABLE_NAME) + ' union' from INFORMATION_SCHEMA.TABLES
要获取每个表的列表以及该表中条目的数量,则可以将其放入临时表中,并查找条目> 1的任何内容。
请记住在最后删除联合。
答案 2 :(得分:0)
select * from
(
SELECT sc.name +'.'+ ta.name TableName
,SUM(pa.rows) RowCnt
FROM sys.tables ta
INNER JOIN sys.partitions pa
ON pa.OBJECT_ID = ta.OBJECT_ID
INNER JOIN sys.schemas sc
ON ta.schema_id = sc.schema_id
WHERE ta.is_ms_shipped = 0 AND pa.index_id IN (1,0)
GROUP BY sc.name,ta.name
ORDER BY SUM(pa.rows) DESC
) as T where RowCnt>0
更多帮助可以帮助您here