Professional Documents
Culture Documents
Database
Exercise 1: Importing an Assembly
Lab answer key
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.
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
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.
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.
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.
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.
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.