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 1: Basic <strong>Defensive</strong> <strong>Database</strong> <strong>Programming</strong> Techniques@DateFrom = '20090101';SET LANGUAGE 'Norsk' ;EXEC dbo.SelectObjectsChangeLogForDateRange@DateFrom = '20090101';-- your actual error message may be different from mine,-- depending on the version of SQL ServerChanged language setting to us_english.(successful output skipped)Changed language setting to Norsk.ObjectID ChangedColumnName ChangedAt OldValue----------- -------------------- ------------- --------------Msg 242, Level 16, State 3, Procedure SelectObjectsChangeLogForDateRange, Line 6The conversion of a char data type to a datetime data typeresulted in an out-of-range datetime value.Listing 1-16: Our stored procedure breaks under Norwegian language settings.Under the Norwegian language settings we receive an error at the point where itattempts to convert 12/31/2099 into a DATETIME string.Note that we are, in fact, quite fortunate to receive an error message right away. Shouldwe, in some other script or procedure, convert '10/12/2008' to DATETIME, SQL Serverwould silently convert this constant to a wrong value and we'd get incorrect results.Listing 1-17 shows how our stored procedure can return unexpected results withoutraising errors; such silent bugs may be very different to troubleshoot.INSERT INTO dbo.ObjectsChangeLog( ObjectID ,ChangedColumnName ,ChangedAt ,40

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

Saved successfully!

Ooh no, something went wrong!