• RamSteve (1/2/2013)


    Hi I am Trying to Build Dynamic Where Clause for Multiple Parameters and they could be Null Also .Please find my

    Query as Below and I need to make AND Operator to be dynamic instead of hard coding it as i was doing it as below

    so the query will look as select * from xyx where cu.unitNumber like '%@SearchValue%' AND cd.CaseDispoID=5 and StartDate >= '12/01/2012' and EndDate<=GetDate()

    Thanks In Advance...

    SELECT @Where =CASE @SearchBy

    WHEN 1 THEN 'WHERE CAST(Ce.CustomerEventID AS VARCHAR)'

    WHEN 2 THEN 'WHERE u.UnitNumber'

    WHEN 3 THEN 'WHERE l.LocationNumber'

    WHEN 4 THEN 'WHERE q.SPName'

    WHEN 5 THEN 'WHERE ce.UserName'

    WHEN 6 THEN 'WHERE c.City + c.StateName'

    WHEN 7 THEN 'WHERE ce.UserName'

    WHEN 8 THEN 'WHERE cu.Name'

    WHEN 9 THEN 'WHERE cu.CustomerNumber'

    ELSE

    'WHERE'

    END

    IF(@SearchValue IS NOT NULL)

    BEGIN

    SET @WhSearchValue ='LIKE ''%'+@SearchValue+'%'' '

    END

    ELSE

    BEGIN

    SET @WhSearchValue =' LIKE''%'++'%'' '

    END

    IF(@CaseDispoID IS NOT NULL)

    BEGIN

    SET @WhCaseDispoID =' AND cd.CaseDispoID='+CAST(@CaseDispoID AS VARCHAR)+''

    END

    ELSE

    BEGIN

    SET @WhCaseDispoID=''

    END

    IF(@StartDate IS NOT NULL)

    BEGIN

    SET @WhStartDate ='AND ce.CreationTime>='''+CAST(@StartDate AS VARCHAR)+''''

    END

    ELSE

    BEGIN

    SET @WhStartDate=''

    END

    IF(@EndDate IS NOT NULL)

    BEGIN

    SET @WhEndDate ='AND ce.CreationTime<='''+CAST(@EndDate AS VARCHAR)+''''

    END

    ELSE

    BEGIN

    SET @WhEndDate ='AND ce.CreationTime <= GetDate()'

    END

    SELECT @SQL= '

    '+@Select+'

    '+@From+'

    '+@Where+'

    '+@WhSearchValue+'

    '+@WhCaseDispoID+'

    '+@WhStartDate+'

    '+@WhEndDate+'

    '

    EXEC (@SQL);

    I see a couple of issues here:

    1. Note where I have made some of your script bold. In those cases, you are missing a blank before either the AND or the LIKE.

    2. This CAST of the DATETIME variable may not give you a format the SQL can easily compare (and it is likely to be truncated):

    CAST(@EndDate AS VARCHAR)

    You should use this instead:

    CAST(VARCHAR(19), @EndDate,120)

    Or if additional precision is required (milliseconds) use:

    CAST(VARCHAR(23), @EndDate,121)

    You can also significantly condense all the statements after the assignment to @WhSearchValue, something like this:

    SELECT @WhCaseDispoID=''

    ,@WhStartDate=''

    ,@WhEndDate=''

    IF @CaseDispoID IS NOT NULL

    SET @WhCaseDispoID =' AND cd.CaseDispoID='+CAST(@CaseDispoID AS VARCHAR)+''

    ELSE IF @StartDate IS NOT NULL

    SET @WhStartDate =' AND ce.CreationTime>='''+CAST(VARCHAR(19), @StartDate,120)+''''

    ELSE IF @EndDate IS NOT NULL

    SET @WhEndDate =' AND ce.CreationTime<='''+CAST(VARCHAR(19), @EndDate,120)+''''

    ELSE SET @WhEndDate =' AND ce.CreationTime <GetDate()'

    That last assuming of course that only one of the 3 input parameters is NOT NULL.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St