30.06.2013 Views

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - 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: Writing Readable <strong>SQL</strong><br />

INNER JOIN Person.ContactType ct<br />

ON vc.ContactTypeID = ct.ContactTypeID<br />

WHERE vc.ContactID = @ContactID )<br />

THEN<br />

(SELECT ct.Name FROM<br />

Purchasing.VendorContact vc<br />

INNER JOIN Person.ContactType ct<br />

ON vc.ContactTypeID =<br />

ct.ContactTypeID<br />

WHERE vc.ContactID = @ContactID)<br />

WHEN EXISTS – – Check for store<br />

( SELECT * FROM Sales.StoreContact sc<br />

INNER JOIN Person.ContactType ct<br />

ON sc.ContactTypeID = ct.ContactTypeID<br />

WHERE sc.ContactID = @ContactID )<br />

THEN<br />

(SELECT ct.Name FROM Sales.StoreContact sc<br />

INNER JOIN Person.ContactType ct<br />

ON sc.ContactTypeID =<br />

ct.ContactTypeID<br />

WHERE ContactID = @ContactID)<br />

ELSE NULL<br />

END ;<br />

SET @ContactType<br />

= CASE- – Check for employee<br />

WHEN EXISTS<br />

( SELECT * FROM HumanResources.Employee e<br />

WHERE e.ContactID = @ContactID )<br />

THEN 'Employee'<br />

WHEN EXISTS – – Check for vendor<br />

( SELECT * FROM Purchasing.VendorContact vc<br />

INNER JOIN Person.ContactType ct<br />

ON vc.ContactTypeID = ct.ContactTypeID<br />

WHERE vc.ContactID = @ContactID )<br />

THEN 'Vendor Contact'<br />

WHEN EXISTS – – Check for store<br />

( SELECT * FROM Sales.StoreContact sc<br />

INNER JOIN Person.ContactType ct<br />

ON sc.ContactTypeID = ct.ContactTypeID<br />

WHERE sc.ContactID = @ContactID )<br />

THEN 'Store Contact'<br />

WHEN EXISTS – – Check for individual consumer<br />

( SELECT * FROM Sales.Individual i<br />

WHERE i.ContactID = @ContactID )<br />

34

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

Saved successfully!

Ooh no, something went wrong!