30.06.2013 Views

SQL Server Execution Plans - Red Gate Software

SQL Server Execution Plans - Red Gate Software

SQL Server Execution Plans - 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 8: Advanced Topics<br />

ON soh.SalesOrderID = sod.SalesOrderID<br />

INNER JOIN Sales.SpecialOffer AS spo<br />

ON sod.SpecialOfferID = spo.SpecialOfferID<br />

WHERE p.BusinessEntityID = @BusinessEntityId<br />

AND spo.[SpecialOfferID] = @SpecialOfferId )<br />

BEGIN<br />

SELECT p.LastName + ', ' + p.FirstName ,<br />

ea.EmailAddress ,<br />

p.Demographics ,<br />

spo.Description ,<br />

spo.DiscountPct ,<br />

sod.LineTotal ,<br />

pr.Name ,<br />

pr.ListPrice ,<br />

sod.UnitPriceDiscount<br />

FROM Person.Person AS p<br />

INNER JOIN Person.EmailAddress AS ea<br />

ON p.BusinessEntityID = ea.BusinessEntityID<br />

INNER JOIN Sales.Customer AS c<br />

ON p.BusinessEntityID = c.PersonID<br />

INNER JOIN Sales.SalesOrderHeader AS soh<br />

ON c.CustomerID = soh.CustomerID<br />

INNER JOIN Sales.SalesOrderDetail AS sod<br />

ON soh.SalesOrderID = sod.SalesOrderID<br />

INNER JOIN Sales.SpecialOffer AS spo<br />

ON sod.SpecialOfferID = spo.SpecialOfferID<br />

INNER JOIN Production.Product pr<br />

ON sod.ProductID = pr.ProductID<br />

WHERE p.BusinessEntityID = @BusinessEntityId<br />

AND sod.[SpecialOfferID] = @SpecialOfferId;<br />

END<br />

-- use different query to return other data set<br />

ELSE<br />

BEGIN<br />

SELECT p.LastName + ', ' + p.FirstName ,<br />

ea.EmailAddress ,<br />

p.Demographics ,<br />

soh.SalesOrderNumber ,<br />

sod.LineTotal ,<br />

pr.Name ,<br />

pr.ListPrice ,<br />

sod.UnitPrice ,<br />

st.Name AS StoreName ,<br />

ec.LastName + ', ' + ec.FirstName<br />

289

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

Saved successfully!

Ooh no, something went wrong!