17.07.2015 Views

Defensive Database Programming - Red Gate Software

Defensive Database Programming - Red Gate Software

Defensive Database Programming - Red Gate Software

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3: Surviving Changes to <strong>Database</strong> ObjectsThis behavior may seem counterintuitive, but it actually makes perfect sense. Let meexplain why, in just two simple steps. Listing 3-24 shows two queries. The first one usesan IN clause; the second is logically equivalent to the first, but the IN clause has beenexpressed using OR predicates.-- A query using the IN clause:SELECT CASE WHEN 1 IN ( 1, 2, NULL ) THEN 'True'ELSE 'Unknown or False'END ;-- its logical eqiuvalent using ORSELECT CASE WHEN ( 1 = 1 )OR ( 1 = 2 )OR ( 1 = NULL ) THEN 'True'ELSE 'Unknown or False'END ;Listing 3-24: A query with an IN clause, and a logically equivalent query using OR.In the second step, we must consider the NOT IN version of our query, convert it touse OR predicates, and then apply DeMorgan's law, which states that for the logicalexpressions P and Q:NOT(P OR Q) = (NOT P) AND (NOT Q)The result is shown in Listing 3-25.-- A query using the NOT IN clause:SELECT CASE WHEN 1 NOT IN ( 1, 2, NULL ) THEN 'True'ELSE 'Unknown or False'END ;-- its logical eqiuvalent using ORSELECT CASE WHEN NOT ( ( 1 = 1 )OR ( 1 = 2 )OR ( 1 = NULL )) THEN 'True'ELSE 'Unknown or False'98

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!