Instructor Inputs Session 12 ¤NIIT Instructor Inputs 12.3 This session includes exercises of Chapter 6, Chapter 7, and additional exercises. Exercise 4 A view has been defined as follows: CREATE VIEW vwSalesOrderDetail AS SELECT oh.SalesOrderID, TerritoryID, TotalDue, OrderQty, ProductID FROM Sales.SalesOrderHeader oh JOIN Sales.SalesOrderDetail od ON oh.SalesOrderID = od.SalesOrderID The following update command gives an error when you update the OrderQty and TerritoryID attributes: UPDATE vwSalesOrderDetail SET OrderQty = 2, TerritoryID = 4 FROM vwSalesOrderDetail WHERE SalesOrderID = 43659 Identify the problem and provide the solution. Solution Since view contains the data from two tables, it is not possible to modify the data of two tables through a view in a single Update statement. To solve the problem, you need to execute two Update statements as follows: UPDATE vwSalesOrderDetail SET OrderQty = 2 FROM vwSalesOrderDetail WHERE SalesOrderID = 43659 UPDATE vwSalesOrderDetail SET TerritoryID = 4 FROM vwSalesOrderDetail WHERE SalesOrderID = 43659 Solutions to Exercises Chapter 6 12.4 Instructor Inputs ¤NIIT Exercise 5 The Store table contains the details of all the stores. The HR Manager of AdventureWorks, Inc. frequently queries the Store table based on the names of the stores. He wants to create the following reports: A report containing the details of all the stores that contain the word 'bike' in their names. A report displaying the names of all the stores containing the phrase 'Bike Store'. Write the query so that the result set is retrieved very promptly. Solution To make searching fast on the Store table, you need to create a full-text index on the table. To solve the problem, you need to execute the following statements: SP_FULLTEXT_DATABASE ENABLE CREATE FULLTEXT CATALOG CAT3 AS DEFAULT CREATE FULLTEXT INDEX ON Sales.Store(Name) KEY INDEX PK_Store_CustomerID To retrieve the details of all the stores containing the word 'bike' in their names, execute the following statement: Select * from Sales.Store Where FreeText (Name, 'Bike') To display the details of all the stores containing the phrase 'Bike Store' in their names, execute the following statement: Select * from Sales.Store Where Contains (Name, '"Bike Store"') Exercise 6 Display the details of all the credit cards that are of type 'SuperiorCard'. The CreditCard table contains a large amount of data. Therefore, the query takes a long time to retrieve the details of the credit card. You need to optimize the execution of the query so that the result set does not take time to be retrieved. ¤NIIT Instructor Inputs 12.5 Solution To retrieve the details of all the credit cards that are of type 'SuperiorCard', you need to create a full-text index on the CreditCard table. This will make the searching very fast. To solve the problem, you need to execute the following statements: SP_FULLTEXT_DATABASE ENABLE CREATE FULLTEXT CATALOG CAT2 AS DEFAULT CREATE FULLTEXT INDEX ON Sales.CreditCard(CardType) KEY INDEX AK_CreditCard_CardNumber Select * from Sales.CreditCard Where Contains (CardType, '"SuperiorCard"') Exercise 7 Display the details of all the currencies that contain the words 'New' and 'Dollar' in their names. These words can be included in any order. In addition, you need to make sure that the query does not take time to execute. Solution You need to create a full-text index on the Currency table to display the currencies containing the words 'New' and 'Dollar' in their names. Full-text index will help in make searching very fast. To solve the problem, you need to execute the following statements: CREATE FULLTEXT INDEX ON Sales.Currency(Name) KEY INDEX AK_Currency_Name Select * from Sales.Currency Where FreeText (Name, '"New" And "Dollar"') Exercise 1 Create a batch that finds the average pay rate of the employees and then lists the details of employees who have a pay rate less than the average pay rate. Chapter 7 12.6 Instructor Inputs ¤NIIT Solution To solve the problem, you need to execute the following statements: DECLARE @avg_rate int SELECT @avg_rate = AVG(rate) FROM HumanResources.EmployeePayHistory SELECT * FROM HumanResources.EmployeePayHistory WHERE rate < @avg_rate GO Exercise 2 Create a function that returns the shipment date of a particular order. Solution To solve the problem, you need to execute the following statement: CREATE FUNCTION Sales.CalShipDate(@SalesOrderID int) RETURNS Datetime AS BEGIN DECLARE @shipDate Datetime SELECT @shipDate = ShipDate FROM Sales.SalesOrderHeader WHERE SalesOrderID = @SalesOrderID IF (@shipDate IS NULL) SET @shipDate = 0 RETURN @shipDate END To execute the preceding function, write the following statement: SELECT SalesOrderID, Sales.CalShipDate(SalesOrderID) AS ShippingDate FROM Sales.SalesOrderHeader Exercise 3 Create a function that returns the credit card number for a particular order. ¤NIIT Instructor Inputs 12.7 Solution To solve the problem, you need to execute the following statement: CREATE FUNCTION Sales.DisplayCardNumber(@SalesOrderID int) RETURNS nvarchar(25) AS BEGIN DECLARE @ret nvarchar(25) SELECT @ret = CardNumber FROM Sales.SalesOrderHeader s JOIN Sales.CreditCard c ON s.CreditCardID = c.CreditCardID WHERE SalesOrderID = @SalesOrderID IF (@ret IS NULL) SET @ret = 0 RETURN @ret END To execute the preceding function, write the following statement: SELECT SalesOrderID, 'Credit Card Number' = Sales.DisplayCardNumber(SalesOrderID) FROM Sales.SalesOrderHeader Exercise 4 Create a function that returns a table containing the customer ID and the name of the customers who are categorized as individual customers (CustomerType = 'I'). The name of the customers will be based on the format of Shortname (only the last name) and Longname (Full name). Solution To solve the problem, you need to execute the following statement: CREATE FUNCTION Sales.IndividualDetails (@format nvarchar(9)) RETURNS @tbl_Individual Table (CustomerID int Primary Key, Name nvarchar(100)) AS BEGIN If (@format = 'LONGNAME') INSERT @tbl_Individual SELECT Cu.CustomerID, FirstName +' '+ LastName FROM Person.Contact AS C 12.8 Instructor Inputs ¤NIIT JOIN Sales.Individual AS I ON C.ContactID = I.ContactID JOIN Sales.Customer AS Cu ON I.CustomerID = Cu.CustomerID WHERE Cu.CustomerType = 'I' ORDER BY LastName, FirstName ELSE IF (@format = 'SHORTNAME') INSERT @tbl_Individual SELECT Cu.CustomerID, LastName FROM Person.Contact AS C JOIN Sales.Individual AS I ON C.ContactID = I.ContactID JOIN Sales.Customer AS Cu ON I.CustomerID = Cu.CustomerID WHERE Cu.CustomerType = 'I' ORDER BY LastName RETURN END To execute the preceding function, write the following statements: SELECT * FROM Sales.IndividualDetails('LONGNAME') SELECT * FROM Sales.IndividualDetails('SHORTNAME') ¤NIIT Instructor Inputs 12.9 This session includes solutions to additional exercises of Chapter 6. Exercise 8 The manager of the production department wants to analyze the products, which contain the exact word 'road' in their description. Write the query so that the result set does not take a long time to execute. Solution The ProductDescription table contains the large amount of data. To retrieve the result set very fast, you need to create a full-text index on the table. To solve the problem, you need to execute the following statements: CREATE FULLTEXT INDEX ON Production.ProductDescription(Description) KEY INDEX AK_ProductDescription_rowguid Select * from Production.ProductDescription Where Contains (Description, 'road') Exercise 9 You need to create a report displaying the details of all the products, which contain the word 'top' near the word 'line' in their description. Write the query to retrieve the desired output. Write the query such that it does not take a long time to execute. Solution The ProductDescription table contains the full-text index. To retrieve the result set very fast, you need to execute the following statement: SELECT Description, ProductDescriptionID FROM Production.ProductDescription WHERE CONTAINS (Description, ' "top" near "line"' ) Solutions to Additional Exercises 12.10 Instructor Inputs ¤NIIT Exercise 10 Display the details of all the stores having the word 'bike' in their name. In addition, the report should contain the details of those stores that have the sales person ID as 277. You need to write the query so that the result set does not take a long time to be retrieved. Solution To solve the problem, you need to execute the following statement: Select * from Sales.Store Where SalesPersonID = 277 and Contains (Name, 'Bike') . Instructor Inputs Session 12 ¤NIIT Instructor Inputs 12. 3 This session includes exercises of Chapter 6, Chapter 7, and additional. be retrieved. ¤NIIT Instructor Inputs 12. 5 Solution To retrieve the details of all the credit cards that are of type 'SuperiorCard', you need to create a full-text index on the CreditCard. Sales.IndividualDetails('LONGNAME') SELECT * FROM Sales.IndividualDetails('SHORTNAME') ¤NIIT Instructor Inputs 12. 9 This session includes solutions to additional exercises of Chapter 6. Exercise 8 The manager