Get the NON NULL and NULL values in a single query

  • HI,

    DECLARE @ID INT

    SET @ID = NULL

    DECLARE @TABLE TABLE(fldID INT)

    INSERT INTO @TABLE

    SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT NULL

    From the above example, i want to return all the records if i pass NULL value to my parameter (@ID) and return the extract row if i pass the 1 or 2.

    SELECT * FROM @TABLE WHERE fldID = 1 /* @ID = 1 */

    fldID

    ----

    1

    SELECT * FROM @TABLE WHERE fldID = @ID /* @ID = NULL */

    fldID

    ----

    1

    2

    NULL

    Can any one help me on this?

  • Try this:

    SELECT *

    FROM @TABLE T

    WHERE ISNULL(T.fldID, '') = CASE WHEN @ID IS NULL THEN ISNULL(T.fldID, '') ELSE @ID END

  • It's a typical catch-all query. I recommend doing this with dynamic SQL.

    See this post[/url] on Gail Shaw's blog for more info and a better solution.

    -- Gianluca Sartori

  • ColdCoffee (4/16/2012)


    Try this:

    SELECT *

    FROM @TABLE T

    WHERE ISNULL(T.fldID, '') = CASE WHEN @ID IS NULL THEN ISNULL(T.fldID, '') ELSE @ID END

    Thanks Man. Thanks for your reply. I got it now. 🙂

  • ColdCoffee (4/16/2012)


    Try this:

    SELECT *

    FROM @TABLE T

    WHERE ISNULL(T.fldID, '') = CASE WHEN @ID IS NULL THEN ISNULL(T.fldID, '') ELSE @ID END

    Be very very careful with this sort of approach. In 2008 you could specify "recompile", but in 2005 you should be using dynamic SQL to ensure the best plan is used.


    --edit--

    Note to self: read whole thread before replying.

    Gianluca Sartori (4/16/2012)


    It's a typical catch-all query. I recommend doing this with dynamic SQL.

    See this post[/url] on Gail Shaw's blog for more info and a better solution.

    That is the excellent resource that I've used in the past as the base for writing queries based on this sort of idea.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • If the input values are either NULL or some value, the we can use this as well.

    IF @ID IS NULL

    BEGIN

    -- We know we need everything when @ID is null

    -- thus we can just use this , anyways a table scan

    -- is waht we actually can get out of this

    SELECT *

    FROM @TABLE T

    END

    ELSE

    BEGIN

    -- This shall for sure use Index, if one exists

    -- and the chance of just an index seek is very high

    SELECT *

    FROM @TABLE T

    WHERE T.fldID = @ID

    END

    Comments in comments 🙂 I hope this produces a decent plan too, and will always be consistent. Using dynamic SQL will also going to give us the same execution plan i believe (havent test it thou)

  • Maybe

    SELECT * FROM @TABLE T

    WHERE T.fldID = @ID or @ID is null

    does good work too.

  • adrian.facio (4/17/2012)


    Maybe

    SELECT * FROM @TABLE T

    WHERE T.fldID = @ID or @ID is null

    does good work too.

    Sorry, but this is a HORRIBLE recommendation!! Please read the referenced link on Catch-All queries earlier in this thread. This type of query is GUARANTEED to get you BAD performance on some/many of your queries!

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • That is why said maybe haha, i will check it out.

  • You are rigth, it produces table scan.

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

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