参数化查询以返回特定值或所有值以及所有NULL

时间:2013-09-25 15:42:46

标签: c# asp.net sql sql-server

我正在处理一个复杂的查询:

SELECT DTH.PointPerson AS Person
    ,(
        CASE 
            WHEN USR.[Deleted] IS NULL
                THEN 'Active'
            ELSE 'Inactive'
            END
        ) AS [Status]
    ,OLT.[Name] AS OrganizationalTeam
    ,PDT.[Name] AS Project
    ,(
        CASE 
            WHEN TSK.NAME IS NULL
                THEN NULL
            WHEN SPT.[Name] + ' - ' + SPT.[Description] IS NULL
                THEN 'KanBan'
            ELSE SPT.[Name] + ' - ' + SPT.[Description]
            END
        ) AS Sprint
    ,COALESCE(STY.[Number], NSS.IncidentNumber) AS Story
    ,TSK.[Name] AS Task
    ,CAST(SUM(CASE 
                WHEN DATEPART(dw, DTH.ActivityDate) = 2
                    THEN DTH.[Hours]
                ELSE 0
                END) AS VARCHAR(20)) AS MondayHours
    ,CAST(SUM(CASE 
                WHEN DATEPART(dw, DTH.ActivityDate) = 3
                    THEN DTH.[Hours]
                ELSE 0
                END) AS VARCHAR(20)) AS TuesdayHours
    ,CAST(SUM(CASE 
                WHEN DATEPART(dw, DTH.ActivityDate) = 4
                    THEN DTH.[Hours]
                ELSE 0
                END) AS VARCHAR(20)) AS WednesdayHours
    ,CAST(SUM(CASE 
                WHEN DATEPART(dw, DTH.ActivityDate) = 5
                    THEN DTH.[Hours]
                ELSE 0
                END) AS VARCHAR(20)) AS ThursdayHours
    ,CAST(SUM(CASE 
                WHEN DATEPART(dw, DTH.ActivityDate) = 6
                    THEN DTH.[Hours]
                ELSE 0
                END) AS VARCHAR(20)) AS FridayHours
    ,CAST(SUM(CASE 
                WHEN DATEPART(dw, DTH.ActivityDate) = 7
                    THEN DTH.[Hours]
                ELSE 0
                END) AS VARCHAR(20)) AS SaturdayHours
    ,CAST(SUM(CASE 
                WHEN DATEPART(dw, DTH.ActivityDate) = 1
                    THEN DTH.[Hours]
                ELSE 0
                END) AS VARCHAR(20)) AS SundayHours
    ,CAST(SUM(DTH.[Hours]) AS VARCHAR(20)) AS TotalHours
FROM DailyTaskHours DTH
LEFT JOIN Task TSK ON DTH.TaskId = TSK.PK_Task
LEFT JOIN Story STY ON TSK.StoryId = STY.PK_Story
LEFT JOIN NonScrumStory NSS ON DTH.NonScrumStoryId = NSS.PK_NonScrumStory
LEFT JOIN Sprint SPT ON STY.SprintId = SPT.PK_Sprint
LEFT JOIN Product PDT ON STY.ProductId = PDT.PK_Product
LEFT JOIN ProductTeamUser PTU ON TSK.ProductTeamUserId = PTU.PK_ProductTeamUser
LEFT JOIN [User] USR ON PTU.UserId = USR.PK_User
LEFT JOIN OrganizationalTeam OLT ON USR.OrganizationalTeamId = OLT.PK_OrganizationalTeam
WHERE DTH.PointPerson LIKE @userParam
    AND ActivityDate >= @startDateParam
    AND ActivityDate <= @endDateParam
    AND OLT.PK_OrganizationalTeam LIKE @orgTeamPK
GROUP BY DTH.PointPerson
    ,PDT.[Name]
    ,SPT.[Name]
    ,SPT.[Description]
    ,STY.[Number]
    ,NSS.IncidentNumber
    ,TSK.[Name]
    ,OLT.[Name]
    ,USR.[Deleted]
HAVING SUM(DTH.[Hours]) > 0

根据用户下拉菜单选择分配上面显示的参数(@orgTeamPK,@ startDateParam等)。

通过C#操作:

SqlDataSource2.SelectParameters["orgTeamPK"].DefaultValue = teamDropDownList.SelectedItem.Value.ToString();

目前,AND OLT.PK_OrganizationalTeam LIKE @orgTeamPK未按预期工作。

有一个下拉菜单如下:选择全部,A队,B队......

当用户选择全选时,我希望它显示所有团队或那些为NULL但未分配给团队的团队。目前,我的尝试是传递'%',但它不会返回NULL。

我可以这样做:

AND OLT.PK_OrganizationalTeam LIKE @orgTeamPK OR OLT.PK_OrganizationalTeam IS NULL

然而,这不会起作用,因为如果用户选择了A队,那么@orgTeamPK将从%重新分配给'A队',它将返回A队和所有空值。

如何在选中时仅让A队返回,但选择全部时所有队员和所有队员都被选中?

1 个答案:

答案 0 :(得分:1)

AND coalesce(OLT.PK_OrganizationalTeam,'') LIKE @orgTeamPK 

这与OR NULL选项不完全相同,因为空字符串与您的团队A / B条件不匹配。