General query intended to be Stored Prodedure using optional parameters

  • I intend to make a stored procedure that accepts several parameters and that has an option not to accept any parameters or some paramters.
    I have made the default value null in all cases.
    The Problem is that when I test it with more that one parameter which is not the default it does not work or gets unconsistent results
    any ideas.
    Would be mostly appreciated
    Thank You
    David

    I am first attempting to write a query that does the action of the stored procedure first.
    The query is below;
    Declare @TicketNo bigint = null
    Declare @Group_Assignment nvarchar(50) = null
    Declare @status nvarchar(50) = null
    Declare @Urgency nvarchar(50) = null
    Declare @Ticket_Category nvarchar(50) = null
    Declare @Requestor nvarchar(50) = null
    Declare @Closure_Reason nvarchar(50).. = null

    select GroupName, T.Status, T.TicketNo, T.Requestor, T.TicketCategory, U.FirstName + ' ' + U.LastName + ' (' + convert(varchar,U.SamAccountName) + ')' as 'Technician',
    T.TicketCreatedDate, DATEDIFF(DAY, T.TicketCreatedDate, getdate()) as 'DAys Open'
    From DB.dbo.HelpDesk_Ticket T
    join DB.HelpDesk_Groups G on T.AssignedGroupID = G.ID
    join DB.VW_AD_ADUsers U on U.Guid = T.AssignedTechGUID
    where

     ((TicketNo = @TicketNo) or @TicketNo is null) and
    ((GroupName = @Group_Assignment) or @Group_Assignment is null) and
    ((Status = @status) or @status is null) and
    ((Urgency = @Urgency) or @Urgency is null) and
    ((Ticket_Category = @Ticket_Category) or  @Ticket_Category is null) and
    ((Requestor = @Requestor or @Requestor is null) and
    ((ClosureReason = @Clusure_Reason) or @Clusure_Reason is null)
    ) option (RECOMPILE)

  • I think you might be getting confused with more than 1 parameter.  If you Declare @TicketNo bigint = 1, @Group_Assignment nvarchar(50) = 'Test', it will only return records where the TicketNo = 1 AND GroupAssignment = 'Test'.  Are you expecting it to return TicketNo = 1 OR GroupAssignment = 'Test?

    For better, quicker answers, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • bdkdavid - Wednesday, June 20, 2018 12:55 PM

    I intend to make a stored procedure that accepts several parameters and that has an option not to accept any parameters or some paramters.
    I have made the default value null in all cases.
    The Problem is that when I test it with more that one parameter which is not the default it does not work or gets unconsistent results
    any ideas.
    Would be mostly appreciated
    Thank You
    David

    I am first attempting to write a query that does the action of the stored procedure first.
    The query is below;
    Declare @TicketNo bigint = null
    Declare @Group_Assignment nvarchar(50) = null
    Declare @status nvarchar(50) = null
    Declare @Urgency nvarchar(50) = null
    Declare @Ticket_Category nvarchar(50) = null
    Declare @Requestor nvarchar(50) = null
    Declare @Closure_Reason nvarchar(50).. = null

    select GroupName, T.Status, T.TicketNo, T.Requestor, T.TicketCategory, U.FirstName + ' ' + U.LastName + ' (' + convert(varchar,U.SamAccountName) + ')' as 'Technician',
    T.TicketCreatedDate, DATEDIFF(DAY, T.TicketCreatedDate, getdate()) as 'DAys Open'
    From DB.dbo.HelpDesk_Ticket T
    join DB.HelpDesk_Groups G on T.AssignedGroupID = G.ID
    join DB.VW_AD_ADUsers U on U.Guid = T.AssignedTechGUID
    where

     ((TicketNo = @TicketNo) or @TicketNo is null) and
    ((GroupName = @Group_Assignment) or @Group_Assignment is null) and
    ((Status = @status) or @status is null) and
    ((Urgency = @Urgency) or @Urgency is null) and
    ((Ticket_Category = @Ticket_Category) or  @Ticket_Category is null) and
    ((Requestor = @Requestor or @Requestor is null) and
    ((ClosureReason = @Clusure_Reason) or @Clusure_Reason is null)
    ) option (RECOMPILE)

    This is commonly known as a 'catch-all query' and there are special considerations and techniques required to make them perform efficiently.
    May I suggest that you read this article to acquaint yourself with them.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Here's a slightly better formatted version of the query:
    DECLARE @TicketNo            AS bigint        = NULL;
    DECLARE @Group_Assignment    AS nvarchar(50) = NULL;
    DECLARE @status                AS nvarchar(50) = NULL;
    DECLARE @Urgency            AS nvarchar(50) = NULL;
    DECLARE @Ticket_Category    AS nvarchar(50) = NULL;
    DECLARE @Requestor            AS nvarchar(50) = NULL;
    DECLARE @Closure_Reason        AS nvarchar(50) = NULL;

    SELECT
        G.GroupName,
        T.[Status],
        T.TicketNo,
        T.Requestor,
        T.TicketCategory,
        U.FirstName + ' ' + U.LastName + ' (' + CONVERT(varchar, U.SamAccountName) + ')' AS 'Technician',
        T.TicketCreatedDate,
        DATEDIFF(day, T.TicketCreatedDate, GETDATE()) AS 'Days Open'
    FROM DB.dbo.HelpDesk_Ticket AS T
        INNER JOIN DB.HelpDesk_Groups AS G
            ON T.AssignedGroupID = G.ID
        INNER JOIN DB.VW_AD_ADUsers AS U
            ON U.Guid = T.AssignedTechGUID
    WHERE    (T.TicketNo = @TicketNo OR @TicketNo IS NULL)
        AND    (G.GroupName = @Group_Assignment OR @Group_Assignment IS NULL)
        AND (T.[Status] = @status OR @status IS NULL)
        AND (T.Urgency = @Urgency OR @Urgency IS NULL)
        AND (T.Ticket_Category = @Ticket_Category OR @Ticket_Category IS NULL)
        AND (T.Requestor = @Requestor OR @Requestor IS NULL)
        AND (T.ClosureReason = @Closure_Reason) OR @Closure_Reason IS NULL)
    OPTION (RECOMPILE);

    As others have suggested, you need to understand that this query, with multiple parameter values, will AND those conditions together, and require that all parameter values be in force.   If that's not your objective, then you need to rethink your WHERE clause.   The problem is that you most likely need dynamic SQL, and yes, this is a "catch-all" query.  Dynamic SQL can help, and will be essential to instead treat each parameter choice as being ORed together with the others.

    You do that by defining the rest of your SQL Query as the beginning value for an nvarchar(max) declared variable.   Then you append only the portions of the WHERE clause that you have not null parameter values for, using OR, and stop worrying about whether or not the parameter value is NULL within the query itself.   That gets resolved in the variable declaration instead.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • Well Basicly, I want a catch all query. I am to create a report that get all values from the query and if the user wants to use one or more of the parameters they shall be able to do so.
    A problem is that the database is poorly designed only two tables have a primary key no foreign keys. Some of the columns have data mismatch one column has a bigint ad the corresponding table same data has a data type of nvarchar(10)
    I am not sure if creating some indexes are going to be of a particular use.
    I created dyanmic query that doesnt appear to work at all.

                Declare @TicketNo bigint = null --17103
            Declare @Group_Assignment nvarchar(50) = null-- 'Desktop Managment'
            Declare @status nvarchar(50) = null --'Open'
            Declare @Urgency nvarchar(50) = null
            
            Declare @Ticket_Category nvarchar(50) = null --'Software'
            Declare @Requestor nvarchar(50) = null
            Declare @Closure_Reason nvarchar(50) = null --'Resolved'
            --Declare @TicketType nvarchar(50) = null;

    DECLARE @SQL nvarchar(2000), @Where nvarchar(1000) = ''
    set @SQL =

            'SELECT GroupName, T.Status, T.TicketNo,T.Requestor,T.TicketCategory,U.FirstName + '' '' +
            U.LastName + '' ('' + CONVERT(VARCHAR,U.SamAccountName) + '')'' AS ''Technician'',
            T.TicketCreatedDate, DATEDIFF(DAY, T.TicketCreatedDate, getdate()) AS ''Days Open''
                FROM [JOTT].[dbo].[HelpDesk_Ticket] T
                JOIN [JOTT].[dbo].[HelpDesk_Groups] G ON T.AssignedGroupID = G.ID
                JOIN [JOTT].[dbo].[VW_ADUsers] U ON U.Guid = T.AssignedTechGUID '

    IF @TicketNo is not null
    SET @Where = @Where + 'AND T.TicketNo = @TicketNo '
    IF @Group_Assignment is not null
    SET @Where = @Where + 'AND T.GroupName = @Group_Assignment '
    IF @status is not null
    SET @Where = @Where + 'AND T.Status = @status '
    IF @Urgency is not null
    SET @Where = @Where + 'AND T.Urgency = @Urgency '
    IF @Ticket_Category is not null
    SET @Where = @Where + 'AND T.Ticket_Category = @Ticket_Category '
    IF @Requestor is not null
    SET @Where = @Where + 'AND T.Requestor = @Requestor '
    IF @Closure_Reason is not null
    SET @Where = @Where + 'AND T.ClosureReason = @Closure_Reason '

    IF LEN(@Where) > 0
    SET @SQL = @SQL + 'WHERE ' + RIGHT(@Where, LEN(@Where)-3)

    EXEC sp_executesql @SQL;

    It only works if default values are used
    Gives Error the scalar varible "@STATUS" works the same for all others

  • This doesn't do the trick either. I got both of these queries from the site mentioned above.
    This seems like it shoulf be a straight forward kind of thing.The first solution. I think should work. I am not sure why it does not.

    I appreciate all the help!

    Thank You
    David


           
        

                Declare     @TicketNo bigint = 15418
                Declare         @Group_Assignment nvarchar(50) = null -- 'Desktop Managment'
                Declare         @status nvarchar(50) = null --'Open'
                Declare         @Urgency nvarchar(50) = null
            
                Declare         @Ticket_Category nvarchar(50) = null --'Software'
                Declare         @Requestor nvarchar(50) = null
                Declare         @Closure_Reason nvarchar(50) = null --'Resolved'
                    --Declare @TicketType nvarchar(50) = null;
            
        
            SELECT GroupName, T.Status, T.TicketNo,T.Requestor,T.TicketCategory,U.FirstName + ' ' + U.LastName + ' (' + CONVERT(VARCHAR,U.SamAccountName) + ')' AS 'Technician',T.TicketCreatedDate, DATEDIFF(DAY, T.TicketCreatedDate, getdate()) AS 'Days Open'
            FROM [db].[dbo].[HelpDesk_Ticket] T
            JOIN [db].[dbo].[HelpDesk_Groups] G ON T.AssignedGroupID = G.ID
            JOIN [db].[dbo].[VW_ADUsers] U ON U.Guid = T.AssignedTechGUID
            where
                (    
                    
                    
                    (TicketNo = CASE WHEN @TicketNo is null THEN TicketNo ELSE @TicketNo END) AND
                    (GroupName = CASE WHEN @Group_Assignment is null THEN GroupName ELSE @Group_Assignment END) AND
                    (Status = CASE WHEN @status is null THEN Status ELSE @status END) AND
                    (Urgency = CASE WHEN @Urgency is null THEN Urgency ELSE @Urgency END) AND
                    (TicketCategory = CASE WHEN @Ticket_Category is null THEN TicketCategory ELSE @Ticket_Category END) AND
                    (Requestor = CASE WHEN @Requestor is null THEN Requestor ELSE @Requestor END) AND
                    (ClosureReason = CASE WHEN @Closure_Reason is null THEN ClosureReason ELSE @Closure_Reason END)
                    
                    --((TicketNo = @TicketNo) or @TicketNo is null) and
                    --((GroupName = @Group_Assignment) or @Group_Assignment is null) and
                    --((Status = @status) or @status is null) and
                    
                    --(Urgency = @Urgency) or @Urgency is null and
                    
                    --(TicketCategory = @Ticket_Category) or @Ticket_Category is null and
                    --(Requestor = @Requestor) or @Requestor is null and
                    --(ClosureReason = @Closure_Reason) or @Closure_Reason is null
                    ----@TicketType is null or (Type = @TicketType)
                    )
            

  • Let's try this one using dynamic SQL and see what happens:
    DECLARE @TicketNo AS bigint = 17103;    --null --
    DECLARE @Group_Assignment AS nvarchar(50) = 'Desktop Managment';    --null--
    DECLARE @status AS nvarchar(50) = 'Open';    --null --
    DECLARE @Urgency AS nvarchar(50) = null
    DECLARE @Ticket_Category AS nvarchar(50) = 'Software';    --null --
    DECLARE @Requestor AS nvarchar(50) = null
    DECLARE @Closure_Reason AS nvarchar(50) = 'Resolved';    --null --
    --Declare @TicketType nvarchar(50) = null;

    DECLARE @SQL AS nvarchar(max), @Where AS nvarchar(max) = N'';
    SET @SQL = N'
    SELECT GroupName, T.Status, T.TicketNo, T.Requestor, T.TicketCategory, U.FirstName + '' '' +
        U.LastName + '' ('' + CONVERT(varchar, U.SamAccountName) + '')'' AS Technician,
        T.TicketCreatedDate, DATEDIFF(day, T.TicketCreatedDate, getdate()) AS [Days Open]
    FROM [JOTT].[dbo].[HelpDesk_Ticket] AS T
        INNER JOIN [JOTT].[dbo].[HelpDesk_Groups] AS G
            ON T.AssignedGroupID = G.ID
        INNER JOIN [JOTT].[dbo].[VW_ADUsers] AS U
            ON U.Guid = T.AssignedTechGUID
    ';

    SET @Where =
        CASE
            WHEN @TicketNo IS NOT NULL THEN N'T.TicketNo = ' + CONVERT(nvarchar(20), @TicketNo) + N'
    '
            ELSE N''
        END +
        CASE
            WHEN @TicketNo IS NULL AND @Group_Assignment IS NOT NULL THEN N'T.GroupName = ''' + @Group_Assignment + N'''
    '
            WHEN @Group_Assignment IS NOT NULL THEN N'    AND T.GroupName = ''' + @Group_Assignment + N'''
    '
            ELSE N''
        END +
        CASE
            WHEN @TicketNo IS NULL AND @Group_Assignment IS NULL AND @status IS NOT NULL THEN N'T.Status = ''' + @status + N'''
    '
            WHEN @status IS NOT NULL THEN N'    AND T.Status = ''' + @status + N'''
    '
            ELSE ''
        END +
        CASE
            WHEN @TicketNo IS NULL AND @Group_Assignment IS NULL AND @status IS NULL AND @Urgency IS NOT NULL
                THEN N'T.Urgency = ''' + @Urgency + N'''
    '
            WHEN @Urgency IS NOT NULL THEN N'    AND T.Urgency = ''' + @Urgency + N'''
    '
            ELSE N''
        END +
        CASE
            WHEN @TicketNo IS NULL AND @Group_Assignment IS NULL AND @status IS NULL AND @Urgency IS NULL
                AND @Ticket_Category IS NOT NULL THEN N'T.Ticket_Category = ''' + @Ticket_Category + N'''
    '
            WHEN @Ticket_Category IS NOT NULL THEN N'    AND T.Ticket_Category = ''' + @Ticket_Category + N'''
    '
            ELSE N''
        END +
        CASE
            WHEN @TicketNo IS NULL AND @Group_Assignment IS NULL AND @status IS NULL AND @Urgency IS NULL
                AND @Ticket_Category IS NULL AND @Requestor IS NOT NULL
                    THEN N'T.Requestor = ''' + @Requestor + N'''
    '
            WHEN @Requestor IS NOT NULL THEN N'    AND T.Requestor = ''' + @Requestor + N'''
    '
            ELSE N''
        END +
        CASE
            WHEN @TicketNo IS NULL AND @Group_Assignment IS NULL AND @status IS NULL AND @Urgency IS NULL
                AND @Ticket_Category IS NULL AND @Requestor IS NULL AND @Closure_Reason IS NOT NULL
                    THEN N'T.ClosureReason = ''' + @Closure_Reason + N''''
            WHEN @Closure_Reason IS NOT NULL THEN N'    AND T.ClosureReason = ''' + @Closure_Reason + N''''
            ELSE N''
        END;

    IF LEN(@Where) > 0
        SET @SQL = @SQL + N'WHERE ' + @Where + N';';

    PRINT @SQL;

    EXEC sp_executesql @SQL;

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • Hello Steve,

    I than you for the Dynamic query it works. Once I added a few primary keys to the DB and added several covered indexes.
    The query above works and my first attempt also works,

    Called Method 1

    -- This works        Method 1
        

    Declare      @TicketNo bigint = null --11907
    Declare         @Group_Assignment nvarchar(50) = null--'Desktop Managment'
    Declare         @status nvarchar(50) = null --'Open'
    Declare         @Urgency nvarchar(50) = null
            
    Declare         @Ticket_Category nvarchar(50) = null-- 'Software'
    Declare         @Requestor nvarchar(50) = null --'BRANDON GREEN'
    Declare         @Closure_Reason nvarchar(50) = null --'Resolved'
                    --Declare @TicketType nvarchar(50) = null;
            
        
            SELECT GroupName, T.Status, T.TicketNo,T.Requestor,T.TicketCategory,U.FirstName + ' ' + U.LastName + ' (' + CONVERT(VARCHAR,U.SamAccountName) + ')' AS 'Technician',T.TicketCreatedDate, DATEDIFF(DAY, T.TicketCreatedDate, getdate()) AS 'Days Open'
            FROM [db].[dbo].[HelpDesk_Ticket] T
            JOIN [db].[dbo].[HelpDesk_Groups] G ON T.AssignedGroupID = G.ID
            JOIN [db].[dbo].[VW_ADUsers] U ON U.Guid = T.AssignedTechGUID
            where
                (                    
                     (@TicketNo is null or TicketNo = @TicketNo) and
                     (@Group_Assignment is null or GroupName = @Group_Assignment) and
                     (@Status is null or Status = @status) and
                    
                    (@Urgency is null or Urgency = @Urgency) and
                    
                    (@Ticket_Category is null or TicketCategory = @Ticket_Category) and
                    (@Requestor is null or Requestor = @Requestor)  and
                    (@Closure_Reason is null or ClosureReason = @Closure_Reason )

                    --@TicketType is null or Type = @TicketType
                )-- End Where Clause
             OPTION (RECOMPILE)

    Method 2 steve query above also works. I had to make a few corrections a few column names, but it also works

    The third method does not work. I was wondering if anyone can give me a reason or show why it does not
    I believe that I am just missing a key point.


    -- This does not work. It does not error either no results either all nulls or  one or multi-value used all with no results      
        --Method 3

    Declare      @TicketNo bigint = null --11907
    Declare         @Group_Assignment nvarchar(50) = null--'Desktop Managment'
    Declare         @status nvarchar(50) = null --'Open'
    Declare         @Urgency nvarchar(50) = null
            
    Declare         @Ticket_Category nvarchar(50) = null-- 'Software'
    Declare         @Requestor nvarchar(50) = null --'BRANDON GREEN'
    Declare         @Closure_Reason nvarchar(50) = null --'Resolved'
                    --Declare @TicketType nvarchar(50) = null;
            
        
            SELECT GroupName, T.Status, T.TicketNo,T.Requestor,T.TicketCategory,U.FirstName + ' ' + U.LastName + ' (' + CONVERT(VARCHAR,U.SamAccountName) + ')' AS 'Technician',T.TicketCreatedDate, DATEDIFF(DAY, T.TicketCreatedDate, getdate()) AS 'Days Open'
            FROM [db].[dbo].[HelpDesk_Ticket] T
            JOIN [db].[dbo].[HelpDesk_Groups] G ON T.AssignedGroupID = G.ID
            JOIN [db].[dbo].[VW_ADUsers] U ON U.Guid = T.AssignedTechGUID
            where
                (    
                    
                    
                    (TicketNo = CASE WHEN @TicketNo is null THEN TicketNo ELSE @TicketNo END) AND
                    (GroupName = CASE WHEN @Group_Assignment is null THEN GroupName ELSE @Group_Assignment END) AND
                    (Status = CASE WHEN @status is null THEN Status ELSE @status END) AND
                    (Urgency = CASE WHEN @Urgency is null THEN Urgency ELSE @Urgency END) AND
                    (TicketCategory = CASE WHEN @Ticket_Category is null THEN TicketCategory ELSE @Ticket_Category END) AND
                    (Requestor = CASE WHEN @Requestor is null THEN Requestor ELSE @Requestor END) AND
                    (ClosureReason = CASE WHEN @Closure_Reason is null THEN ClosureReason ELSE @Closure_Reason END)
                    
                
                )-- End Where Clause
             OPTION (RECOMPILE)

    I wanted to thank you all for your help

    David

  • bdkdavid - Friday, June 22, 2018 1:18 PM

    Hello Steve,

    I than you for the Dynamic query it works. Once I added a few primary keys to the DB and added several covered indexes.
    The query above works and my first attempt also works,

    Called Method 1

    -- This works        Method 1
        

    Declare      @TicketNo bigint = null --11907
    Declare         @Group_Assignment nvarchar(50) = null--'Desktop Managment'
    Declare         @status nvarchar(50) = null --'Open'
    Declare         @Urgency nvarchar(50) = null
            
    Declare         @Ticket_Category nvarchar(50) = null-- 'Software'
    Declare         @Requestor nvarchar(50) = null --'BRANDON GREEN'
    Declare         @Closure_Reason nvarchar(50) = null --'Resolved'
                    --Declare @TicketType nvarchar(50) = null;
            
        
            SELECT GroupName, T.Status, T.TicketNo,T.Requestor,T.TicketCategory,U.FirstName + ' ' + U.LastName + ' (' + CONVERT(VARCHAR,U.SamAccountName) + ')' AS 'Technician',T.TicketCreatedDate, DATEDIFF(DAY, T.TicketCreatedDate, getdate()) AS 'Days Open'
            FROM [db].[dbo].[HelpDesk_Ticket] T
            JOIN [db].[dbo].[HelpDesk_Groups] G ON T.AssignedGroupID = G.ID
            JOIN [db].[dbo].[VW_ADUsers] U ON U.Guid = T.AssignedTechGUID
            where
                (                    
                     (@TicketNo is null or TicketNo = @TicketNo) and
                     (@Group_Assignment is null or GroupName = @Group_Assignment) and
                     (@Status is null or Status = @status) and
                    
                    (@Urgency is null or Urgency = @Urgency) and
                    
                    (@Ticket_Category is null or TicketCategory = @Ticket_Category) and
                    (@Requestor is null or Requestor = @Requestor)  and
                    (@Closure_Reason is null or ClosureReason = @Closure_Reason )

                    --@TicketType is null or Type = @TicketType
                )-- End Where Clause
             OPTION (RECOMPILE)

    Method 2 steve query above also works. I had to make a few corrections a few column names, but it also works

    The third method does not work. I was wondering if anyone can give me a reason or show why it does not
    I believe that I am just missing a key point.


    -- This does not work. It does not error either no results either all nulls or  one or multi-value used all with no results      
        --Method 3

    Declare      @TicketNo bigint = null --11907
    Declare         @Group_Assignment nvarchar(50) = null--'Desktop Managment'
    Declare         @status nvarchar(50) = null --'Open'
    Declare         @Urgency nvarchar(50) = null
            
    Declare         @Ticket_Category nvarchar(50) = null-- 'Software'
    Declare         @Requestor nvarchar(50) = null --'BRANDON GREEN'
    Declare         @Closure_Reason nvarchar(50) = null --'Resolved'
                    --Declare @TicketType nvarchar(50) = null;
            
        
            SELECT GroupName, T.Status, T.TicketNo,T.Requestor,T.TicketCategory,U.FirstName + ' ' + U.LastName + ' (' + CONVERT(VARCHAR,U.SamAccountName) + ')' AS 'Technician',T.TicketCreatedDate, DATEDIFF(DAY, T.TicketCreatedDate, getdate()) AS 'Days Open'
            FROM [db].[dbo].[HelpDesk_Ticket] T
            JOIN [db].[dbo].[HelpDesk_Groups] G ON T.AssignedGroupID = G.ID
            JOIN [db].[dbo].[VW_ADUsers] U ON U.Guid = T.AssignedTechGUID
            where
                (    
                    
                    
                    (TicketNo = CASE WHEN @TicketNo is null THEN TicketNo ELSE @TicketNo END) AND
                    (GroupName = CASE WHEN @Group_Assignment is null THEN GroupName ELSE @Group_Assignment END) AND
                    (Status = CASE WHEN @status is null THEN Status ELSE @status END) AND
                    (Urgency = CASE WHEN @Urgency is null THEN Urgency ELSE @Urgency END) AND
                    (TicketCategory = CASE WHEN @Ticket_Category is null THEN TicketCategory ELSE @Ticket_Category END) AND
                    (Requestor = CASE WHEN @Requestor is null THEN Requestor ELSE @Requestor END) AND
                    (ClosureReason = CASE WHEN @Closure_Reason is null THEN ClosureReason ELSE @Closure_Reason END)
                    
                
                )-- End Where Clause
             OPTION (RECOMPILE)

    I wanted to thank you all for your help

    David

    You might well have NULL values for one of the tested columns in that WHERE clause, which would end up requiring that said column be equal to NULL, which can't ever be possible because NULL can't be "equal" to anything.   If any one of the tested columns is always NULL, that could cause the problem with that form of the query.

    EDIT:  It would also be possible that for every row, at least one of the tested columns is NULL, which would be sufficient to cause the same issue.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • bdkdavid - Friday, June 22, 2018 1:18 PM

    ... <<code segments deleted>>

    I wanted to thank you all for your help

    David

    You're very welcome.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply