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 1: Basic <strong>Defensive</strong> <strong>Database</strong> <strong>Programming</strong> TechniquesSET ManagerID = @ManagerIDWHERE EmployeeID = @EmployeeID ;Listing 1-20: Using unambiguous search criteria.As long as EmployeeID is the primary key on the dbo.Employee table, this procedurewill work correctly.The problem of ambiguous updatesThe results of data modifications may be unpredictable in the hands of the carelessprogrammer. Let's consider a very common requirement: populating a permanent tablefrom a staging table. First of all, let's create our permanent table, Codes, and a stagingtable, CodesStaging, as shown in Listing 1-21. Note that CodesStaging does nothave a primary key. This is very common for staging tables, because data is often loadedinto such tables before detecting duplicates and other data integrity violations.CREATE TABLE dbo.Codes(Code VARCHAR(5) NOT NULL ,Description VARCHAR(40) NOT NULL ,CONSTRAINT PK_Codes PRIMARY KEY ( Code )) ;GOCREATE TABLE dbo.CodesStaging(Code VARCHAR(10) NOT NULL ,Description VARCHAR(40) NOT NULL) ;GOListing 1-21: Creating the Codes and CodesStaging tables.45

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

Saved successfully!

Ooh no, something went wrong!