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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3: Surviving Changes to <strong>Database</strong> ObjectsGOSELECT '123456' ,NULL ,'Users manual for some gadget' ;SELECT BarcodeFROM dbo.ShipmentItemsWHERE ShipmentBarcode = '123654'AND Barcode NOT IN ( SELECT BarcodeFROM dbo.ShipmentItemsWHERE ShipmentBarcode ='123456' ) ;Barcode------------------------------(0 row(s) affected)Listing 3-22: Now that the Barcode column accepts NULL, our NOT IN queryno longer works as expected.This can often seem like a very subtle bug; sometimes the query works as expected,but sometimes it does not. In fact, the behavior is completely consistent. Every timethe subquery inside the NOT IN clause returns at least one NULL, then the queryreturns nothing. Listing 3-23 shows a much simpler script that demonstrates thisbehavior very clearly.SELECT CASE WHEN 1 NOT IN ( 2, 3 ) THEN 'True'ELSE 'Unknown or False'END ,CASE WHEN 1 NOT IN ( 2, 3, NULL ) THEN 'True'ELSE 'Unknown or False'END ;----------------------True Unknown or FalseListing 3-23: NOT IN queries will work differently when there are NULLsin the subquery.97

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

Saved successfully!

Ooh no, something went wrong!