表名称及其使用SQL Server的行数

时间:2016-02-26 10:55:55

标签: sql-server

为了获得下面的表格结果,我使用了以下查询,

select DISTINCT  
    DO.Name as TableName, 
    '' as 'TotalRecords',
    RDM.DataObjectId as Id,
    COALESCE(DO.GroupId, 0) as GroupId,
    dog.Name as GroupName
from 
    [User] as U
inner join 
    UserGroupMap UGM on UGM.UserId = U.Id
inner join 
    UserGroupRole UGR on UGR.UserGroupId = UGM.UserGroupId
inner join 
    RoleDoMap RDM on RDM.RoleId = UGR.RoleId 
                  and RDM.Id in (select RoleDOMapId 
                                 from RoleDOPermission DP)
inner join 
    Client C on C.Id = RDM.ClientId
inner join 
    [EDGE].[DataObject] DO on DO.Id = RDM.DataObjectId
left join
    [EDGE].DataObjectGroup dog on dog.Id = DO.GroupId
where 
    UGM.UserId = '46' 
    and C.SchemaName = 'EDGE'

My query result like below.

我需要获得每个表的总行数。我无法从上面的查询中获得计数。

欢迎任何建议。

谢谢和问候

3 个答案:

答案 0 :(得分:0)

SELECT *, COUNT(1) OVER ()
FROM ...

答案 1 :(得分:0)

使用group by

select DISTINCT  DO.Name as TableName, COUNT(DO.Name) as 'TotalRecords',RDM.DataObjectId as Id,COALESCE(DO.GroupId, 0) as GroupId,dog.Name as GroupName

    from [User] as U
    inner join UserGroupMap UGM on UGM.UserId = U.Id
    inner join UserGroupRole UGR on UGR.UserGroupId = UGM.UserGroupId
    inner join RoleDoMap RDM on RDM.RoleId = UGR.RoleId 
    inner join RoleDOPermission dp on dp.RoleDOMapId = RDM.Id
    inner join Client C on C.Id = RDM.ClientId
    inner join [EDGE].[DataObject] DO on DO.Id = RDM.DataObjectId
    LEFT JOIN [EDGE].DataObjectGroup dog on dog.Id = DO.GroupId
    group by DO.Name,RDM.DataObjectId,DO.GroupId,dog.Name
    where UGM.UserId='46' and C.SchemaName = 'EDGE'

答案 2 :(得分:0)

SELECT  QUOTENAME(Schema_name(t.Schema_id)) + 
        QUOTENAME(object_name(st.object_id)) AS TableName
       ,SUM(st.row_count)                    AS Total_Rows

FROM       sys.dm_db_partition_stats st
INNER JOIN sys.tables t ON st.object_id = t.object_id
WHERE st.index_id < 2
AND t.is_ms_shipped = 0
GROUP BY QUOTENAME(Schema_name(t.Schema_id)) + 
        QUOTENAME(object_name(st.object_id))