2016-11-16 54 views
0

我有一個SQL查詢,下面我想獲得的所有表表名,總的行數

select Outer.Table_Name, Column_Name, sub.rowcount 
from INFORMATION_SCHEMA.columns Outer 
-- left join (select sub.TableName, count(*) As rowcount from LeadBase)sub 
-- sub.TableName = Outer.Table_Name 
where Table_Name = 'LeadBase' and Column_Name in ('CreatedOn','ModifiedOn') 
order by 2 

的COUNT(*),但顯然上述不起作用,因爲你不能在使用表名子查詢(註釋掉了),有沒有辦法做到這一點,所以我有從外部查詢沿tableName count(*)結果?

回答

0
SELECT  SCHEMA_NAME(A.schema_id) + '.' + A.Name, SUM(B.rows) AS 'RowCount' 

FROM sys.objects中甲INNER JOIN sys.partitions乙ON A.object_id = B.object_id WHERE A.type =「U'GROUP BY A.schema_id,A.Name

+0

這看起來很有希望,我將在分區表有時與查詢 – abs786123

+0

喜它的工作原理打左右,但受到了很多它相乘後回覆你,object_ID不止一次出現。可能需要帶一個max()其中一個ID,你會知道使用哪一個? – abs786123

+0

然後在分區表中僅對多個object_ID中的所有行進行求和 – Mansoor

0

託管到通過讓你看着分區表的想法來排序

SELECT DB_NAME() AS [Current Database], 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 pa.index_id IN (1,0) and Type = 'U' 
GROUP BY sc.name,ta.name 
ORDER BY SUM(pa.rows) DESC