1. Querying and Managing Data Using SQL Server 2005
Objectives
In this session, you will learn to:
Understand managed code
Create managed database objects
Define the Hypertext Transfer Protocol endpoints
Implement the Hypertext Transfer Protocol endpoints for Web
services
Ver. 1.0 Session 16 Slide 1 of 31
2. Querying and Managing Data Using SQL Server 2005
Introduction to SQL Server CLR Integration
CLR integration:
Allows the database developer to write the code in any of
the .NET supported languages
Allows to run managed code within a database
Provides the following functions and services required for
program execution
Ver. 1.0 Session 16 Slide 2 of 31
3. Querying and Managing Data Using SQL Server 2005
Identifying the Need for Managed Code
Managed database objects can be created in the following
situations:
To implement complicated programming logics
To access external resources
To implement a CPU-intensive functionality that can run more
efficiently as compared to the managed code.
T-SQL statements can be used in the following situations:
To perform data access and manipulation operations that can
be done using the T-SQL statements.
To implement programming logic tat can be easily
implemented using T-SQL programming constructs.
Ver. 1.0 Session 16 Slide 3 of 31
4. Querying and Managing Data Using SQL Server 2005
Just a minute
Which of the following is supported by .NET and not by
T-SQL?
1. Writing queries
2. Creating procedures
3. Object-Orientation
4. Writing triggers
Answer:
3. Object-Orientation
Ver. 1.0 Session 16 Slide 4 of 31
5. Querying and Managing Data Using SQL Server 2005
Importing and Configuring Assemblies
Assemblies:
Are created to attach the managed code
Are created using the CREATE ASSEMBLY command
Syntax:
CREATE ASSEMBLY assembly_name
FROM { <client_assembly_specifier> |
<assembly_bits>
[ ,...n ] }
[ WITH PERMISSION_SET =
{ SAFE | EXTERNAL_ACCESS | UNSAFE } ]
Let’s see how…
Ver. 1.0 Session 16 Slide 5 of 31
6. Querying and Managing Data Using SQL Server 2005
Just a minute
Which of the following PERMISSION_SET will you use to
access another database server?
1. SAFE
2. EXTERNAL_ACCESS
3. UNSAFE
Answer:
2. EXTERNAL_ACCESS
Ver. 1.0 Session 16 Slide 6 of 31
7. Querying and Managing Data Using SQL Server 2005
Creating Managed Database Objects
Managed database objects can be of the following types:
Stored Procedures
Functions
Triggers
UDTs
Ver. 1.0 Session 16 Slide 7 of 31
8. Querying and Managing Data Using SQL Server 2005
Creating Managed Database Objects (Contd.)
Managed stored procedure:
Is implemented by creating a procedure that refers to an
imported assembly
Syntax:
CREATE PROCEDURE <Procedure Name>
AS EXTERNAL NAME <Assembly Identifier>.<Type
Name>.<Method Name>,
Let’s see how…
Ver. 1.0 Session 16 Slide 8 of 31
9. Querying and Managing Data Using SQL Server 2005
Creating Managed Database Objects (Contd.)
Managed function:
Is implemented by creating a function that refers to an
imported assembly
Syntax:
CREATE FUNCTION <Function Name>
(
<Parameter List>
)
RETURNS <Return Type>
AS EXTERNAL NAME <Assembly Identifier>.<Type
Name>.<Method Name>
Let’s see how…
Ver. 1.0 Session 16 Slide 9 of 31
10. Querying and Managing Data Using SQL Server 2005
Creating Managed Database Objects (Contd.)
Managed trigger:
Is implemented by creating a trigger that refers to an imported
assembly
Syntax:
CREATE TRIGGER <TriggerName>
ON <Table or View> <FOR | INSTEAD OF | AFTER>
< INSERT | UPDATE | DELETE >
AS EXTERNAL NAME <Assembly Identifier>.<Type
Name>.<Method Name>
Let’s see how…
Ver. 1.0 Session 16 Slide 10 of 31
11. Querying and Managing Data Using SQL Server 2005
Creating Managed Database Objects (Contd.)
Managed user-defined type:
Is created by using the CREATE TYPE command
Syntax:
CREATE TYPE [ schema_name. ] type_name
{
FROM base_type [ (precision [ , scale ] ) ]
[ NULL | NOT NULL ]
| EXTERNAL NAME assembly_name[.class_name]
}
Let’s see how…
Ver. 1.0 Session 16 Slide 11 of 31
12. Querying and Managing Data Using SQL Server 2005
Just a minute
When will you use managed code instead of T-SQL?
1. When you need to write queries.
2. When you need to access external resources.
3. When you need to perform an administrative task on the
database.
Answer:
2. When you need to access external resources.
Ver. 1.0 Session 16 Slide 12 of 31
13. Querying and Managing Data Using SQL Server 2005
Demo: Implementing Managed User-Defined Types
• Problem Statement:
The management of AdventureWorks, Inc. has decided that
they want to include the details of the spouse of employees in
the database. The application that is used to enter the
employee detail will accept the name and date of birth of the
spouse of an employee. In addition, it will concatenate the two
values separated by a ";". As a database developer, you need
to store the spouse details in the following format:
Spouse Name: <name of the spouse> ; Spouse Date of Birth :
<date of birth>
To implement this, you have decided to create a managed
user-defined data type. How will you create this data type?
Ver. 1.0 Session 16 Slide 13 of 31
14. Querying and Managing Data Using SQL Server 2005
Demo: Implementing Managed User-Defined Types (Contd.)
Solution:
To solve the preceding problem, you need to perform the
following tasks:
1. Enable CLR in the database.
2. Create an assembly.
3. Create a managed database user-defined data type.
4. Create a table that will implement the user-defined data type.
5. Verify the output.
Ver. 1.0 Session 16 Slide 14 of 31
15. Querying and Managing Data Using SQL Server 2005
Introduction to Service-Oriented Architecture (SOA)
SOA:
Is an extension of distributed computing based on the
request/reply design pattern
Modularizes the business logic of an application and presents
them as services
Allows to create objects, such as, Web services that can be
accessed from heterogeneous systems
Ver. 1.0 Session 16 Slide 15 of 31
16. Querying and Managing Data Using SQL Server 2005
Introduction to Web Services
Flash presentation: Introduction to Web Services
Web Service is a collection of methods that provide
programmable logic used by client applications over the
Internet.
SQL Server 2005 provides native XML Web services by
using the following open standards:
Hypertext Transfer Protocol (HTTP)
Simple Object Access Protocol (SOAP)
Web Services Definition Language (WSDL)
Ver. 1.0 Session 16 Slide 16 of 31
17. Querying and Managing Data Using SQL Server 2005
Just a minute
Which of the following describes the Web services?
1. WSDL
2. SOAP
3. UDDI
Answer:
1. WSDL
Ver. 1.0 Session 16 Slide 17 of 31
18. Querying and Managing Data Using SQL Server 2005
Just a minute
Which of the following helps in finding a Web service?
1. WSDL
2. SOAP
3. UDDI
Answer:
3. UDDI
Ver. 1.0 Session 16 Slide 18 of 31
19. Querying and Managing Data Using SQL Server 2005
Identifying the Role of HTTP Endpoints in Native Web Service Architecture
HTTP Endpoint:
Is the gateway through which HTTP-based clients can query
the database server
Created for use with SQL Server 2005 can listen and receive
requests on the TCP port (port 80)
Ver. 1.0 Session 16 Slide 19 of 31
20. Querying and Managing Data Using SQL Server 2005
Identifying the Role of HTTP Endpoints in Native Web Service Architecture (Contd.)
HTTP Endpoint Architecture:
Ver. 1.0 Session 16 Slide 20 of 31
21. Querying and Managing Data Using SQL Server 2005
Just a minute
On which of the following ports does the SQL Server listen
for HTTP requests?
1. 80
2. 90
3. 70
Answer:
1. 80
Ver. 1.0 Session 16 Slide 21 of 31
22. Querying and Managing Data Using SQL Server 2005
Creating HTTP Endpoints
Involves the following tasks:
1. Creating the required database code to access the data.
2. Creating an HTTP Endpoint using the CREATE ENDPOINT
statement.
Ver. 1.0 Session 16 Slide 22 of 31
23. Querying and Managing Data Using SQL Server 2005
Creating HTTP Endpoints (Contd.)
Syntax:
CREATE ENDPOINT endpoint_name
STATE = { STARTED | STOPPED | DISABLED }
AS HTTP (
AUTHENTICATION =( { BASIC | DIGEST |
INTEGRATED | NTLM | KERBEROS },
PATH = 'url', PORTS = (CLEAR) )
FOR SOAP(
[ { WEBMETHOD [ 'namespace' .] 'method_alias'
( NAME = 'database.owner.name'
[ , SCHEMA = { NONE | STANDARD | DEFAULT } ]
[ , FORMAT = { ALL_RESULTS | ROWSETS_ONLY } ])
} [ ,...n ] ]
[ BATCHES = { ENABLED | DISABLED } ]
[ , WSDL = { NONE | DEFAULT | 'sp_name' } ]
Let’s see how…
Ver. 1.0 Session 16 Slide 23 of 31
24. Querying and Managing Data Using SQL Server 2005
Just a minute
While creating an HTTP Endpoint, which of the following
format will you use to return only the result set to the user?
1. ROWSET_ONLY
2. ALL_RESULT
3. NONE
Answer:
1. ROWSET_ONLY
Ver. 1.0 Session 16 Slide 24 of 31
25. Querying and Managing Data Using SQL Server 2005
Demo: Implementing HTTP Endpoints
• Problem Statement:
The database server of AdventureWorks, Inc. is located at
Bothell. The organization has various offices located at various
locations spread across the globe.
According to the requirements, the users need to access the
data of all the employees at various locations. Users might
need to use PDAs or mobile phones to access these details.
As a database developer, you have decided to implement a
Web service that allows the users to access the data using the
Internet.
How will you implement this service inside the AdventureWorks
database?
Ver. 1.0 Session 16 Slide 25 of 31
26. Querying and Managing Data Using SQL Server 2005
Demo: Implementing HTTP Endpoints (Contd.)
Solution:
To solve the preceding problem, you need to perform the
following tasks:
1. Create a procedure.
2. Create an HTTP Endpoint for SOAP.
3. Verify the creation of HTTP endpoint. `
Ver. 1.0 Session 16 Slide 26 of 31
27. Querying and Managing Data Using SQL Server 2005
Summary
In this session, you learned that:
The database objects created in any of the .NET supported
languages are called managed database objects.
CLR integration provides the following benefits:
• Better programming model
• Common development environment
• Ability to define data types
T-SQL can be used to perform data access and manipulation
operations that can be implemented using the programming
constructs provided by T-SQL.
Ver. 1.0 Session 16 Slide 27 of 31
28. Querying and Managing Data Using SQL Server 2005
Summary (Contd.)
Managed database objects can be used in the following
situations:
To implement complicated programming logic for which you can
reuse the functionality provided by the .NET base class libraries.
To access external resources, such as calling a Web service or
accessing the file system.
To implement a CPU-intensive functionality that can run more
efficiently as compared to the managed code.
By default, the SQL Server does not allow running managed
code on the server.
Before creating a managed database object in your database,
the CLR integration feature should be enabled in the database
using the sp_configure stored procedure.
The .NET code that is used to create the managed database
objects is compiled in .NET assemblies, .dll or .exe files.
To create a managed database object, first first the.NET
assemblies are imported in the database engine.
Ver. 1.0 Session 16 Slide 28 of 31
29. Querying and Managing Data Using SQL Server 2005
Summary (Contd.)
The assemblies in the database engine can be given any of the
following three permissions:
SAFE
EXTERNAL_ACCESS
UNSAFE
Managed stored procedure can be created using the CREATE
PROCEDURE command.
Managed function can be created using the CREATE
FUNCTION command.
Managed trigger can be created using the CREATE TRIGGER
command.
Managed data type can be created using the CREATE TYPE
command.
A Web service is the piece of code that is exposed over the
Internet.
Ver. 1.0 Session 16 Slide 29 of 31
30. Querying and Managing Data Using SQL Server 2005
Summary (Contd.)
Web services have following advantages:
• Interoperability
• Multilanguage support
• Reusing existing applications
SOAP is a standard communication protocol to interchange
information in a structured format in a distributed environment.
WSDL is a markup language that describes a Web service.
UDDI provides a standard mechanism to register and discover
a Web service.
HTTP endpoints allow you to create and use Web services
within the SQL Server.
Before creating an HTTP endpoint, you need to first create
stored procedures or functions that form a Web service.
Ver. 1.0 Session 16 Slide 30 of 31
31. Querying and Managing Data Using SQL Server 2005
Summary (Contd.)
HTTP endpoints provide the users with a connecting point
through which they can access the implemented functions.
You can create HTTP endpoints by using the CREATE
ENDPOINT statement.
Ver. 1.0 Session 16 Slide 31 of 31
Hinweis der Redaktion
Start the session by sharing the objectives with the students.
In this slide, you need to explain the students about CLR. As they have already read about CLR in the previous module, you need not to go into the details of CLR. In addition, you also need to explain them that CLR has now been incorporated in the SQL Server itself. Also explain the students about the benefits of integrating CLR inside SQL Server itself. You can use the examples provided in the Student Guide to clarify the concept to the students. Additional Inputs T-SQL has been the conventional language to write database objects, such as stored procedure, triggers and functions. SQL Server 2005 provides the new possibilities to the database developer by integrating .NET Framework in it. SQL Server 2005 allows the database developers to write stored procedures, triggers, user-defined types, user-defined aggregates, and user-defined functions in any of the .NET supported language, build a dll, register and use it inside a SQL Server.
In this slide, you need to explain the need for managed code to the students. You need to explain the limitations of T-SQL to the students, and how those limitations can be overcome by the introducing managed code in SQL Server. Additional Input T-SQL is a great language to manipulate and query data from a database server, and working on set-based operations. It provides a number of database features
Example: You have created a managed code and saved it as CLRIntegration.dll. To create an assembly for this dll, you can write the following code: CREATE ASSEMBLY CLRIntegration FROM ‘ C:\\CLRIntegration\\CLRIntegration.dll’ WITH PERMISSION_SET = SAFE
For example, the employees data stored in the database need to be converted into an XML file. For this purpose, you need to create a function that will read the data from the table and write it inside the XML file. As you need to access an external file, you need to create a managed procedure.
For Example, you need a function that accepts Employee Id as an input, does all the processing, and finally displays the actual salary of that employee for current month. The processing includes, adding all the components of the salary, checking and adjusting the salary according to the attendance, and deducting the provident fund contribution. Writing this code in T-SQL is very complicated, as it involves a number of decisions and a number of processing. You can create a managed function that accepts employee code and returns the actual salary.
For Example, you want that whenever a new database object is added in the AdventureWorks database, the same should be added in the list of the database objects available with the database administrator. The list of database objects is stored in a text file with the DBA.
For Example, You need to create a data type to store the zip code of addresses of the employees. You also want if user enters valid code, the name of the state is stored in the column else an error message should be displayed. Creating such a UDT is very complicated in SQL Server, for this purpose you will create a Managed UDT.
Do not go into the depth of SOAD, WSDL, and UDDI. Just try to clear the concept of Web Services to the student.
Example: You have created a function that returns the day as number. To create a new endpoint named sqlEndpoint, you need to use the following code in SQL Server. CREATE ENDPOINT sql_endpoint STATE = STARTED AS HTTP( PATH = '/sql', AUTHENTICATION = (INTEGRATED ), PORTS = ( CLEAR ), SITE = 'SERVER' ) FOR SOAP ( WEBMETHOD 'GetSqlInfo' (name='master.dbo.xp_msver', SCHEMA=STANDARD ), WEBMETHOD 'DayAsNumber' (name='master.sys.fn_MSdayasnumber'), WSDL = DEFAULT, SCHEMA = STANDARD, DATABASE = 'master', NAMESPACE = 'http://tempUri.org/' );
You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.