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 2: Code Vulnerabilities due to SQL Server MisconceptionsSELECT @ID ,RAND() * 1000000 ,'asdf' ;GOSET @ID = @ID + 1 ;END ;Listing 2-21: Adding 100K rows to the wide table.If we select the data from this table, without providing an ORDER BY clause, the datamay be retrieved in the order in which it was inserted, as was the case when I ran thequery shown in Listing 2-22 on my server.SELECT TOP ( 1000 )IDFROM dbo.WideTable ;ID-----------12345(snip)Listing 2-22: Without an ORDER BY clause, the rows are returned in the order theywere inserted.However, there is no guarantee that this script will work on your server in exactly thesame way that it did on mine. Many people assume that, in the absence of an ORDER BYclause, the data will be returned "in the order of the primary key," or "in the order of theclustering index," or "in the order it was inserted." None of these assumptions is correct.Listing 2-23 demonstrates that after we have added a non-clustered index, the result ofour query is different.73

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

Saved successfully!

Ooh no, something went wrong!