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 2: Code Vulnerabilities due to SQL Server MisconceptionsIt also creates a Messages table and populates it with test data (if you already have atable with this name, from running examples in Chapter 1, you will need to drop it). Tomake sure that the example consistently executes in the same way on different servers, Ihad to populate the table with a large number of wide rows; this is why we need to insertone million rows, and that every row has a CHAR(200) column. As a result, the code maytake some time to complete. Note also that only one row out of one million has a validdate in the MessageDateAsVarcharColumn column.-- helper tableCREATE TABLE dbo.Numbers(n INT NOT NULLPRIMARY KEY) ;GODECLARE @i INT ;SET @i = 1 ;INSERT INTO dbo.Numbers( n )SELECT 1 ;WHILE @i < 1000000BEGIN;INSERT INTO dbo.Numbers( n )SELECT n + @iFROM dbo.Numbers ;SET @i = @i * 2 ;END ;GOCREATE TABLE dbo.Messages(MessageID INT NOT NULLPRIMARY KEY ,-- in real life the following two columns-- would have foreign key constraints;59

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

Saved successfully!

Ooh no, something went wrong!