Instructor Inputs - Session 15 doc

10 133 0
Instructor Inputs - Session 15 doc

Đang tải... (xem toàn văn)

Thông tin tài liệu

Instructor Inputs Session 15 ¤NIIT Instructor Inputs 15.3 This session includes exercises of Chapter 8, Chapter 9, and Chapter 10. Exercise 1 The management of AdventureWorks, Inc. has decided that no user should be able to change the prices of the products. In addition, management wants that all the attempts to change the price should be saved in a temporary table, Temp. John, the database developer has been asked to make the significant changes in the database to implement this policy. What can John do to achieve the same? Solution To accomplish this task, John needs to create an update trigger on the ProductCostHistory table. In addition, he also needs to create a temporary table to store the changes in it. To create the temporary table, John needs to execute the following statements in the SQL Server Management Studio: CREATE TABLE Temp ( ProductID int, AttChangeCost money, AttTime datetime ) To create the update trigger on the ProductCostHistory, John needs to execute the following statements: CREATE TRIGGER updTrigger ON [Production].[ProductCostHistory] INSTEAD OF UPDATE AS BEGIN DECLARE @PID AS int DECLARE @COST AS money SELECT @pid = ProductID, @cost = StandardCost FROM Deleted INSERT INTO Temp VALUES(@pid, @cost, getdate()) SELECT 'Sorry you can not change the price of a Product' END Solutions to Exercises Chapter 8 15.4 Instructor Inputs ¤NIIT Tip To verify the update trigger on the ProductCostHistory, John needs to execute the following statements: UPDATE [Production].[ProductCostHistory] SET StandardCost = 55 WHERE ProductID = 707 Database engine will display the following message: ‘Sorry you can not change the price of a Product’ Exercise 2 The management of AdventureWorks, Inc. wants that whenever the pay rate of an employee is modified, its effect on the monthly salary of the employee should be displayed. John, a database developer at AdventureWorks, has been asked to resolve this problem. Help John to find out appropriate solution. Monthly Salary = Rate * PayFrequency * 30 Solution To accomplish this task, John needs to create an update trigger on the HumanResources.EmployeePayHistory. To create the trigger, he needs to execute the following statements: CREATE TRIGGER updTrigger ON HumanResources.EmployeePayHistory FOR UPDATE AS BEGIN DECLARE @rate AS money DECLARE @frq AS int SELECT @rate = Rate, @frq = PayFrequency FROM Inserted SELECT @rate * @frq * 30 AS 'Monthly Salary' END ¤NIIT Instructor Inputs 15.5 Note To verify the trigger, John needs to execute the following statement to update the salary for employee with EmployeeID as 160: UPDATE HumanResources.EmployeePayHistory SET Rate = Rate + 5 WHERE EmployeeID = 160 The output will show the monthly salary after the change is done. Exercise 1 The AdventureWorks database maintains the sales details in the SalesOrderHeader and SalesOrderDetails tables. As a database developer of AdventureWorks, Inc., you need to retrieve all the order numbers for the sales accounts in the following format: AccountNumber OrderNumbers 10-4020-000676 SO43659 SO44305 SO45061 SO45779 10-4020-000117 SO43660 SO47660 SO49857 SO51086 How will you retrieve data in the given format? To complete this exercise, you need to provide the AccontDetails.dll file to the students. The file is available in the Datafiles_for_students\QMDS2005\Chapter 09 folder in the TIRM CD. Solution To solve the preceding problem, you need to perform the following tasks: 1. Create the assembly. 2. Create the function. 3. Verify the result. Chapter 9 15.6 Instructor Inputs ¤NIIT Task 1: Creating the Assembly To create the assembly, you need to perform the following steps: 1. Open the Microsoft SQL Server Management Studio window. 2. In the Query Editor window, enter the following statement: CREATE ASSEMBLY AccDetails FROM ‘C:\AccountDetails.dll’ WITH PERMISSION_SET = UNSAFE The AccountDetails.dll file is created by using the following code: using System; using System.Data; using System.Data.Sql; using Microsoft.SqlServer.Server; using System.Collections; using System.Data.SqlTypes; using System.Data.SqlClient; using System.Diagnostics; using System.IO; namespace AccountDetails { public class Det { [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)] public static SqlString details(SqlString acn) { SqlConnection connection = new SqlConnection("context connection = true"); SqlString sr = new SqlString(); connection.Open(); SqlCommand cmd = new SqlCommand("select salesorderid from sales.salesorderheader where accountnumber = '"+acn.ToString()+"'", connection); SqlDataReader dr; dr = cmd.ExecuteReader(); while (dr.Read()) { sr = sr.ToString() + " " + dr[0].ToString(); } dr.Close(); ¤NIIT Instructor Inputs 15.7 Note connection.Close(); return sr; } } } 3. Press the F5 key to execute the statement. Task 2: Creating the Function To create the function for the required scenario, you need to perform the following steps: 1. Type the following statement in the Query Editor window: CREATE FUNCTION AccDetails (@acn nvarchar(20)) RETURNS nvarchar(200) AS EXTERNAL NAME [AccDetails].[AccountDetails.Det].details 2. Press the F5 key to execute the statement. Task 3: Verifying the Result To verify the result of the function, execute the following statement: SELECT dbo.AccDetails(‘10-4020-000676’) AS Details Exercise 1 Multiple business partners of AdventureWorks need to use the details of all the sales stores stored in the AdventureWorks database server. The details include the name of the store, name of the store owner, and e-mail address of the owner. As per the company’s security policy, access to company’s databases cannot be provided to any external entity. As a database developer, how will you make the list available to the other organizations without any additional overhead? If you are unable to view the Web service in the Object Explorer, you need to refresh the Object Explorer window several times. Chapter 10 15.8 Instructor Inputs ¤NIIT Solution To provide the required functionality to other organizations, you need to perform the following tasks: 1. Create a stored procedure. 2. Create an HTTP endpoint. 3. Verify the creation of HTTP endpoint. Task 1: Creating a Stored Procedure To make the data available to the clients, you need to create a database object that will provide the data. This database object will be converted into web method and will be hosted in a web service. In current scenario, we need to create a stored procedure that will provide the data from the related tables. To create the stored procedure, you need to perform the following steps: 1. Type the following statement in the Query Editor window of the Microsoft SQL Server Management Studio window: CREATE PROCEDURE HTTPProc AS BEGIN SELECT tbl1.NAME, tbl2.FirstName, tbl2.EmailAddress FROM Sales.Store AS tbl1 JOIN Sales.StoreContact AS tbl3 ON tbl1.CustomerID = tbl3.CustomerID JOIN Person.Contact AS tbl2 ON tbl3.ContactID = tbl2.ContactID END 2. Press the F5 key to execute the statement. Task 2: Creating an HTTP Endpoint Once you have created the database object to provide the data, you can create the access mechanism to provide the data (HTTP endpoint). To create a new HTTP endpoint, you need to perform the following steps: 3. Type the following statement to create an HTTP endpoint named DetailsStore: CREATE ENDPOINT DetailsStore STATE = STARTED AS HTTP( PATH = '/stores', AUTHENTICATION = (INTEGRATED ), PORTS = ( CLEAR ), SITE = 'SERVER' ) FOR SOAP ( ¤NIIT Instructor Inputs 15.9 Note WEBMETHOD 'StoreDetail' (name='AdventureWorks.dbo.HTTPProc', SCHEMA=STANDARD ), WSDL = DEFAULT, SCHEMA = STANDARD, DATABASE = 'AdventureWorks', NAMESPACE = 'http://tempUri.org/' ); 4. Press the F5 key to execute the statement. Task 3: Verifying the Creation of HTTP Endpoint To verify the creation of HTTP endpoint, you can view the details of the Web method in the Microsoft SQL Sever Management Studio window. For this, you need to expand the Server ObjectsÆEndpointsÆSOAP nodes in the Object Explorer window. You will view the HTTPProc Web method listed under the SOAP node. If you are unable to view the web service in the Object Explorer, you need to refresh it several times. 15.10 Instructor Inputs ¤NIIT . Instructor Inputs Session 15 ¤NIIT Instructor Inputs 15. 3 This session includes exercises of Chapter 8, Chapter 9, and Chapter. sales accounts in the following format: AccountNumber OrderNumbers 1 0-4 02 0-0 00676 SO43659 SO44305 SO45061 SO45779 1 0-4 02 0-0 00117 SO43660 SO47660 SO49857 SO51086 How will you retrieve data in. PayFrequency FROM Inserted SELECT @rate * @frq * 30 AS 'Monthly Salary' END ¤NIIT Instructor Inputs 15. 5 Note To verify the trigger, John needs to execute the following statement to update

Ngày đăng: 31/07/2014, 15:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan