You are on page 1of 4

Module 8 Lab: Implementing Managed Code in the

Database
Exercise 1: Importing an Assembly
Lab answer key

Importing the AWorksUtilities assembly


You must perform the following steps to import the AWorksUtilities assembly:
1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, and then click
SQL Server Management Studio.
2. In the Connect to Server dialog box, specify the values in the following table, and then click
Connect.
Property Value
Server type Database Engine
Server name MIAMI
Authentication Windows Authentication

3. On the File menu, point to Open, click Project/Solution, and then open the
AWorks_CLR.ssmssln solution in D:\Labfiles\Starter.
4. If Solution Explorer is not visible, click Solution Explorer on the View menu.
5. In Solution Explorer, double-click the Initialize.sql query file.
6. On the toolbar, click Execute, and confirm that no errors occur.
7. In Solution Explorer, double-click the ImportAssembly.sql query file.
8. Under the Enable CLR integration comment, add the following code.

sp_configure 'clr enabled', 1


GO
RECONFIGURE
GO

9. On the toolbar, click Execute, verifying that the statements executed successfully.
10. Under the Create assembly comment, add the following code.

USE AdventureWorks
CREATE ASSEMBLY AWorksUtilities
FROM
'D:\Labfiles\Starter\AWorksUtilities.dll'

11. Select the CREATE ASSEMBLY statement, and then on the toolbar, click Execute. In the
query results, verify that the statements executed successfully.
12. On the File menu, click Save ImportAssembly.sql.
13. Keep the SQL Server Management Studio solution open. You will use it in the next exercise.
Exercise 2: Creating Managed Database Objects
Lab answer key

Creating the SaveXML managed stored procedure

You must perform the following steps to create the SaveXML stored procedure:
1. In Solution Explorer, double-click the SaveXML.sql query file.
2. Locate the Create managed stored procedure comment, and add the following code.

USE AdventureWorks
GO
CREATE PROCEDURE dbo.SaveXML
( @XmlData xml,
@FileName nvarchar(100) )
AS EXTERNAL NAME AWorksUtilities.StoredProcedures.SaveXML
GO

3. Select the CREATE PROCEDURE statement, and then on the toolbar, click Execute,
verifying that the statements executed successfully.
4. Under the Test managed stored procedure comment, select the DECLARE, SET, and
EXEC statements. Execute these statements, and then confirm that an error message is
displayed because the assembly does not have the required permissions to write to the file
system.
5. Under the Change security permissions to allow file access comment, add the following
code.

ALTER ASSEMBLY AWorksUtilities


WITH PERMISSION_SET = EXTERNAL_ACCESS
GO
6. Select the ALTER ASSEMBLY statement, and then on the toolbar, click Execute. In the
query results, verify that the statements executed successfully.
7. Under the Retest managed stored procedure comment, select the DECLARE, SET, and
EXEC statements. Execute these statements, and then confirm that no errors occur this time.
8. On the File menu, click Save SaveXML.sql.
9. Keep the SQL Server Management Studio solution open. You will use it in the next
procedure.
10. In Windows Explorer, verify the existence of the Output.xml file in the D:\Labfiles\Starter
folder. Double-click the file to display the XML in Internet Explorer. Close Internet Explorer
and Windows Explorer when you have confirmed that the file contains XML.
Creating the GetLongDate managed user-defined function
You must perform the following steps to create the GetLongDate user-defined function:

1. In Solution Explorer, double-click the GetLongDate.sql query file.


2. Under the Create managed user-defined function comment, add the following code.

USE AdventureWorks
GO
CREATE FUNCTION dbo.GetLongDate ( @DateVal datetime )
RETURNS nvarchar(50)
AS EXTERNAL NAME AWorksUtilities.UserDefinedFunctions.GetLongDate
GO

3. Select the CREATE FUNCTION statement, and then on the toolbar, click Execute, verifying
that the statements executed successfully.
4. Under the Test managed user-defined function comment, select the SELECT statement. On
the toolbar, click Execute, and then confirm that the records are displayed with a long
OrderDate value.
5. On the File menu, click Save GetLongDate.sql.
6. Keep the SQL Server Management Studio solution open. You will use it in the next
procedure.

Creating the EmailChange managed trigger


You must perform the following steps to create the EmailChange trigger:

1. In Solution Explorer, double-click the EmailChange.sql query file.


2. Under the Create trigger comment, add the following code.

USE AdventureWorks
GO
CREATE TRIGGER EmailChange ON Person.Contact
FOR UPDATE
AS
EXTERNAL NAME AWorksUtilities.Triggers.EmailChange
GO

3. Select the CREATE TRIGGER statement, and then on the toolbar, click Execute, verifying
that the statements executed successfully.
4. Under the Test trigger comment, select the first UPDATE statement. On the toolbar, click
Execute, and then confirm that a message is displayed, noting the new e-mail address in the
results pane.
5. Select the second SELECT statement. On the toolbar, click Execute, and then confirm that
there is no message because the statement did not update the e-mail address. This is because
the managed code checks to see which columns were updated and performs the trigger logic
only if the e-mail column is modified.
6. On the File menu, click Save EmailChange.sql.
7. Keep the SQL Server Management Studio solution open. You will use it in the next
procedure.

Creating the Concatenate managed aggregate function


You must perform the following steps to create the Concatenate aggregate function:

1. In Solution Explorer, double-click the Concatenate.sql query file.


2. Under the Create aggregate function comment, add the following code.

USE AdventureWorks
GO
CREATE AGGREGATE Concatenate(@input nvarchar(4000))
RETURNS nvarchar(4000)
EXTERNAL NAME AWorksUtilities.Concatenate
GO

3. Select the CREATE AGGREGATE statement, and then on the toolbar, click Execute,
verifying that the statements executed successfully.
4. Under the Test aggregate function comment, select the SELECT statement. On the toolbar,
click Execute, and then confirm that orders are displayed as a concatenated string for each
account in the results pane.
5. On the File menu, click Save Concatenate.sql.
6. Keep the SQL Server Management Studio solution open. You will use it in the next
procedure.

Creating the IPAddress managed user-defined type


You must perform the following steps to create the IPAddress type:

1. In Solution Explorer, double-click the IPAddress.sql query file.


2. Under the Create managed user-defined type comment, add the following code.

USE AdventureWorks
GO
CREATE TYPE IPAddress
EXTERNAL NAME AWorksUtilities.IPAddress
GO

3. Select the CREATE TYPE statement, and then on the toolbar, click Execute, verifying that
the statements executed successfully.
4. Under the Test managed user-defined type comment, select the DECLARE, SET, and
SELECT statements. On the toolbar, click Execute, and then confirm that three result sets are
displayed showing the IPAddress value in various forms.
5. On the File menu, click Save IPAddress.sql.
6. Close the SQL Server Management Studio solution.

You might also like