SQL Server 2008中的列错误无效

时间:2014-09-08 08:54:43

标签: sql sql-server sql-server-2008

我正在尝试运行以下查询,

DECLARE @p_UserName as nvarchar(100)
DECLARE @p_Department as int
DECLARE @p_Section as int
DECLARE @p_SubSection as int
DECLARE @p_PermissionGroup as int
DECLARE @p_DistributionGroup as int
DECLARE @p_Permission as nvarchar(100)
DECLARE @p_IfPerChecked as bit
DECLARE @p_Role as int

SET @p_UserName = ''
SET @p_Department = NULL
SET @p_Section = NULL
SET @p_SubSection = NULL
SET @p_PermissionGroup = NULL
SET @p_DistributionGroup = NULL
SET @p_Permission = ''
SET @p_Role = NULL

SELECT Users.EnglishName,
    (SELECT Designation.TitleEnglish FROM Designation WHERE Users.Designation = Designation.ID) AS [Role],
    (SELECT Department.TitleEnglish FROM Department WHERE Users.DepartmentID = Department.ID) AS [Department],
    (SELECT Section.TitleEnglish FROM Section WHERE Users.SectionID = Section.SectionID) AS [Section],
    (SELECT SubSection.TitleEnglish FROM SubSection WHERE Users.SubSectionID = SubSection.SubSectionID) AS [Sub-Section],
    (SELECT Groups.TitleEnglish FROM Groups WHERE 
        (SELECT UserRights.GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID) = Groups.GroupID
        AND Groups.IsDistribution = 0
    ) AS [Permissions Group],
    (SELECT Groups.TitleEnglish FROM Groups WHERE 
        (SELECT UserRights.GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID) = Groups.GroupID
        AND Groups.IsDistribution = 1
    ) AS [Distribution Group],
    (CASE
        WHEN Users.ApplyUserRights = 1 THEN dbo.fn_GetUserPermissions('Users', Users.UserID)
        WHEN Users.ApplyUserRights = 0 THEN dbo.fn_GetUserPermissions('Groups', (SELECT GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID))
    END) AS [Permissions]
FROM Users WHERE 
    ((@p_UserName <> NULL OR @p_UserName <> '') AND @p_UserName = Users.UserName) OR
    ((@p_Department <> NULL OR @p_Department <> 0) AND @p_Department = Users.DepartmentID) OR
    ((@p_Section <> NULL OR @p_Section <> 0) AND @p_Section = Users.SectionID) OR
    ((@p_SubSection <> NULL OR @p_SubSection <> 0) AND @p_SubSection = Users.SubSectionID) OR
    ((@p_PermissionGroup <> NULL OR @p_PermissionGroup <> 0) AND @p_PermissionGroup = (
            SELECT Groups.GroupID FROM Groups WHERE 
                (SELECT UserRights.GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID) = Groups.GroupID
                AND Groups.IsDistribution = 0
        )) OR
    ((@p_DistributionGroup <> NULL OR @p_DistributionGroup <> 0) AND @p_DistributionGroup = (
            SELECT Groups.GroupID FROM Groups WHERE 
                (SELECT UserRights.GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID) = Groups.GroupID
                AND Groups.IsDistribution = 1
        )) OR
    (1 = CASE
        WHEN Users.ApplyUserRights = 1 AND @p_Permission = 'AllowChangePassword' THEN 0
        ELSE dbo.fn_CheckPemission(@p_Permission, UserID)
    END) OR
    ((@p_Role <> NULL OR @p_Role <> 0) AND @p_Role = Users.Designation) OR
    (1 = 1)
GROUP BY CASE @GroupBy
    WHEN 'DepartmentID' THEN Users.DepartmentID
    WHEN 'SectionID' THEN Users.SectionID
    WHEN 'SubSectionID' THEN Users.SubSectionID
    ELSE Users.EnglishName
END

我一直收到错误

  

专栏&#39; Users.EnglishName&#39;在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

请帮忙!

2 个答案:

答案 0 :(得分:1)

与消息状态类似,您需要按EnglishName列进行分组,除非它在聚合函数中使用(Max或sum或类似)。

答案 1 :(得分:0)

问题从您的错误消息中可见一斑。在聚合函数的情况下使用GROUP BY。