• j-1064772 (1/13/2014)


    You are right, I really meant INTERSECT only.

    EXCEPT was a mistake. (Also, [font="Courier New"]SELECT ProductID FROM Product WHERE NOT ProductID IN (SELECT ProductID FROM WorkOrder)[/font] also does yield the same results as EXCEPT).

    Thanks for you suggested link.

    Regards

    I find it easier to read using EXCEPT. Also, using NOT IN will return an empty result set if the subquery contains a NULL.

    Try these two and see what I mean.

    SELECT ProductID

    FROM Product

    WHERE ProductID NOT IN

    (

    SELECT ProductID FROM WorkOrder

    UNION ALL

    SELECT NULL

    )

    SELECT ProductID

    FROM Product

    EXCEPT

    (

    SELECT ProductID FROM WorkOrder

    UNION ALL

    SELECT NULL

    )

    The differences are subtle but very important to understand.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/