用于从CRM 2013中检索所有用户和默认团队的SQL

时间:2015-10-01 10:52:51

标签: sql-server dynamics-crm-2013

我需要能够检索所有用户以及他们所属的业务单位的默认团队。

除了用于团队和BU的同一名称外,我如何识别业务单位的默认团队。是否有一个标志将其标识为默认团队?

我需要的输出是以下列

  • 域名
  • 团队名称+团队名称的GUID(默认团队的名称) 他们所属的BU。)。

这是我的查询。

SELECT dbo_SystemUser.DomainName, (dbo_Team.Name + ' ' + dbo_Team.TeamId)
FROM (dbo_Team INNER JOIN (dbo_TeamMembership INNER JOIN dbo_SystemUser ON dbo_TeamMembership.SystemUserId = dbo_SystemUser.SystemUserId) ON dbo_Team.TeamId = dbo_TeamMembership.TeamId) INNER JOIN dbo_BusinessUnit ON dbo_Team.BusinessUnitId = dbo_BusinessUnit.BusinessUnitId
WHERE dbo_Team.Name = dbo_BusinessUnit.Name

2 个答案:

答案 0 :(得分:2)

对于BU的默认团队,

Team表格为IsDefaultTRUE

它也包含在FilteredTeam视图中。

解决OP问题的查询:

-- Pay attention to the FROMs, tables ignore Security Roles.
-- To respect security, query the filtered views
-- (FilteredSystemUser, FilteredTeam, FilteredTeamMembership)
-- On the other hand, querying tables is much faster

SELECT U.DomainName as [Domain Name], T.Name as [Team Name], T.TeamId as [Team ID]
FROM SystemUser U 
    INNER JOIN TeamMembership TM ON U.SystemUserId = TM.SystemUserId
    INNER JOIN Team T ON TM.TeamId = T.TeamId
WHERE T.IsDefault = 1

答案 1 :(得分:1)

这应该这样做。

SELECT FilteredSystemUser.domainname, FilteredTeam.name, FilteredTeam.teamid
FROM FilteredSystemUser
    INNER JOIN FilteredBusinessUnit ON FilteredSystemUser.businessunitid = FilteredBusinessUnit.businessunitid
    INNER JOIN FilteredTeam ON FilteredTeam.businessunitid = FilteredBusinessUnit.businessunitid
        WHERE FilteredTeam.isdefault = 1

您还需要使用Filtered Views.