Chapter 5 ADVANCED DATA RETRIEVAL AND MODIFICATION 257

Chapter 5 ADVANCED DATA RETRIEVAL AND MODIFICATION 257 PersonID column, so SQL Server gives you an error if you don t specify which table the column comes from. SQL Server also gives you an error if you write this: SELECT PersonID, FirstName, LastName, ProductID, .QtyPurchased FROM Person INNER JOIN Sales ON Person.PersonID = Sales.PersonID This results in an error of Ambiguous column name PersonID . Once again, because the PersonID column appears more than once in the set of joined tables, you need to specify which PersonID you want, even though they are both going to have the same value after the join. The code sample is also representative of one way to indent and style the SQL. It s valid to put the entire SQL statement on one line, but it s also valid to wear dark socks with sandals, striped shorts, and a plaid shirt. Use a consistent style when you are writing queries; it makes them easier for you to read and for others to read. That s all well and good, but how can you get a list of the addresses for all the people to whom you ve sold things? That goes something like this: SELECT Address.* FROM Person INNER JOIN Sales ON Person.PersonID = Sales.PersonID INNER JOIN PersonAddress ON PersonAddress.PersonID = Person.PersonID INNER JOIN Address ON PersonAddress.AddressID = Address.AddressID The Address.* in that statement specifies that you want to return all the fields in the Address table, but you don t want the fields in the rest of the tables. This is a convenient shortcut. Here s another neat way to reduce typing: SELECT A.* FROM Person P INNER JOIN Sales S ON P.PersonID = S.PersonID INNER JOIN PersonAddress PA ON PA.PersonID = P.PersonID INNER JOIN Address A ON PA.AddressID = A.AddressID

If you looking for unlimited one inclusive web hosting plan please check cheap web hosting website.

Comments are closed.