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 6: Common Problems with Data Integrity@NewBarcode VARCHAR(20) ;-- retrieve the barcode before updateSELECT @ItemLabel = ItemLabel ,@OldBarcode = BarcodeFROM deleted ;-- retrieve the barcode after updateSELECT @NewBarcode = BarcodeFROM inserted ;PRINT 'old and new barcode as stored in variables' ;SELECT @OldBarcode AS OldBarcode ,@NewBarcode AS NewBarcode ;-- determine if the barcode changedIF ( ( @OldBarcode @NewBarcode )OR ( @OldBarcode IS NULLAND @NewBarcode IS NOT NULL)OR ( @OldBarcode IS NOT NULLAND @NewBarcode IS NULL))BEGIN ;INSERT INTO dbo.ItemBarcodeChangeLog( ItemLabel ,ModificationDateTime ,OldBarcode ,NewBarcode)VALUES ( @ItemLabel ,CURRENT_TIMESTAMP ,@OldBarcode ,@NewBarcode) ;END ;END ;Listing 6-46: The Items_LogBarcodeChange trigger logs changes made to theBarcode column of the Items table.194

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

Saved successfully!

Ooh no, something went wrong!