19 – 21 MARCH 2006 Riyadh, Saudi Arabia. XML and Web Services support in SQL Server 2005 Michael Storey Xpertise Training Ltd (UK)

Slides:



Advertisements
Similar presentations
Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates.
Advertisements

XML: Extensible Markup Language
Module 17 Tracing Access to SQL Server 2008 R2. Module Overview Capturing Activity using SQL Server Profiler Improving Performance with the Database Engine.
Advantage Data Dictionary. agenda Creating and Managing Data Dictionaries –Tables, Indexes, Fields, and Triggers –Defining Referential Integrity –Defining.
G O B E Y O N D C O N V E N T I O N WORF: Developing DB2 UDB based Web Services on a Websphere Application Server Kris Van Thillo, ABIS Training & Consulting.
Inside Data Access with SQLXML: Architecture Guide Rolandas Gricius MCT, MCSE, MCSD Adapted From.
Introduction to XML CS348 Information System Guest Lecture Hazem Elmeleegy.
Introduction to Structured Query Language (SQL)
The XML data type in Microsoft SQL Server. The XML Type Microsoft SQL Server offers a special data type – XML – Used in tables, etc. – Basically a (long)
Peoplesoft: Building and Consuming Web Services
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Introduction to Structured Query Language (SQL)
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
Module 9 Designing an XML Strategy. Module 9: Designing an XML Strategy Designing XML Storage Designing a Data Conversion Strategy Designing an XML Query.
Working with SQL and PL/SQL/ Session 1 / 1 of 27 SQL Server Architecture.
SQL Server 2000 and XML Erik Veerman Consultant Intellinet Business Intelligence.
Copying, Managing, and Transforming Data With DTS.
4/20/2017.
DAT304 Leveraging XML and HTTP with Sql Server Irwin Dolobowsky Program Manager Webdata Group.
Module 17 Storing XML Data in SQL Server® 2008 R2.
2.2 SQL Server 2005 的 XML 支援功能. Overview XML Enhancements in SQL Server 2005 The xml Data Type Using XQuery.
Database Application Security Models
Integrating XML with Microsoft SQL Server ©NIITeXtensible Markup Language/Lesson 9/Slide 1 of 31 Objectives In this lesson, you will learn to: * Generate.
Using XML in SQL Server 2005 NameTitleCompany. XML Overview Business Opportunity The majority of all data transmitted electronically between organizations.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
ASP.NET Programming with C# and SQL Server First Edition
Native Support for Web Services  Native Web services access  Enables cross platform interoperability  Reduces middle-tier dependency (no IIS)  Simplifies.
XML in SQL Server Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form.
Sofia, Bulgaria | 9-10 October Using XQuery to Query and Manipulate XML Data Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
DBSQL 14-1 Copyright © Genetic Computer School 2009 Chapter 14 Microsoft SQL Server.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Module 9 Designing and Implementing Stored Procedures.
Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up a Linked Server Environment Working with Linked Servers.
DAT 330 Building Web Services with SQL Server and SQLXML 3.0 Alexander Vaschillo Lead Program Manager WebData XML Group Microsoft Corporation.
5/24/01 Leveraging SQL Server 2000 in ColdFusion Applications December 9, 2003 Chris Lomvardias SRA International
XML Web Services Architecture Siddharth Ruchandani CS 6362 – SW Architecture & Design Summer /11/05.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 18 Object Database Management Systems. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Motivation for object.
Module 18 Querying XML Data in SQL Server® 2008 R2.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Copyright© 2005 Oracle Corp.1 SQL/XML Jim Melton USA: Oracle Corp. JTC1 SC32N1632.
Mike Jackson EPCC OGSA-DAI Architecture + Extensibility OGSA-DAI Tutorial GGF17, Tokyo.
Oracle 11g: SQL Chapter 4 Constraints.
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
Session 11 Creating Tables and Using Data Types. RDBMS and Data Management/Session 11/2 of 40 Session Objectives Define the data types and list the categories.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
XML and Database.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
SQL Server 2005: Extending the Type System with XML.
ODS – Introduction to Web Services and BPEL Vakgroep Informatietechnologie Web Services & BPEL Design of Distributed Software.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Chapter 14 1 Chapter 14 Storing and Retrieving XML in SQL Server 2000 November 6, 2001 Sook-Kyo Kwon.
Session 1 Module 1: Introduction to Data Integrity
SQLXML XML Technology For SQL Server Brian Moore Developer and Platform Strategy Group Microsoft Corporation.
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.
 CONACT UC:  Magnific training   
Interstage BPM v11.2 1Copyright © 2010 FUJITSU LIMITED INTEGRATION.
In this session, you will learn to: Understand managed code Create managed database objects Define the Hypertext Transfer Protocol endpoints Implement.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Generating XML Data from a Database Eugenia Fernandez IUPUI.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
XML: Extensible Markup Language
Using XML in SQL Server and Azure SQL Database
Querying and Transforming XML Data
Dirt, Spit, and Happy FLWOR
Data Model.
Chapter 11 Managing Databases with SQL Server 2000
Presentation transcript:

19 – 21 MARCH 2006 Riyadh, Saudi Arabia

XML and Web Services support in SQL Server 2005 Michael Storey Xpertise Training Ltd (UK)

Outline The XML Data Type Schema validated and untyped xml XML type indexes FOR XML and OpenXML enhancements Using XQuery with the XML data type Updating the XML data type with XQuery DML SQL Server 2005 Web Service Support Accessing HTTP endpoints with.NET clients

XML in a relational database XML can be stored as text loses much of value of XML representation XML can decomposed into multiple relational tables allows use of relational technologies XML can be stored as an xml data type allows use of XML technologies

Using the XML data type CREATE TABLE xml_tab ( the_id INTEGER, xml_col XML) CREATE PROCEDURE transform XML OUTPUT) AS... CREATE FUNCTION simple NVARCHAR(max)) RETURNS XML AS XML

XML column XML column can store well-formed XML XML 1.0 recommendation documents or fragments

XML schema in database XML schemas used by XML data types must be in database Create XML SCHEMA COLLECTION collection name associated with XML instance Create XML SCHEMA COLLECTION requires literal schemas CREATE XML SCHEMA COLLECTION geocoll AS '<xs:schema... targetNamespace= urn:geo>... ' literal schema targetNamespace CREATE TABLE Locations ( location xml(geocoll)... ) reference to schema collection

Using strongly typed XML CREATE TABLE point_tab( id int IDENTITY primary key, -- geocoll include schema for 'urn:geo' namespace thepoint xml(CONTENT geocoll) GO -- this works, schema-valid Point INSERT INTO point_tab VALUES( ' ') -- this insert fails, value foo is not a dim (integer) INSERT INTO point_tab VALUES( ' 10 foo ')

XML indexes You can create XML INDEXes on an XML column optimizes XML Queries on the column primary XML index must be created first (node table) three specialized index types also available VALUE – optimizes content queries PATH – optimizes structure queries PROPERTY – optimizes name/value pair predicates CREATE TABLE xml_tab ( id integer primary key, doc xml) GO CREATE PRIMARY XML INDEX xml_idx on xml_tab (doc) GO

Enhancements to SELECT...FOR XML SELECT FOR XML is an extension to Transact-SQL usually produces a stream in SQL Server 2005, it can also produce an XML data type use TYPE keyword after FOR XML can be used with RAW, AUTO formats can be used to query a "collection" of documents xml = SELECT * FROM authors FOR XML AUTO, TYPE

FOR XML PATH SELECT FOR XML PATH allows shaping of output FOR XML AUTO, RAW allow little shaping FOR XML EXPLICIT is complex to write FOR XML PATH uses path-syntax in column aliases allows "creation" of elements simpler than XML EXPLICIT can specify namespaces with XMLNAMESPACES function

XML PATH example WITH XMLNAMESPACES('urn:authors' AS au) SELECT au_id as au_fname as [name/firstname], au_lname as [name/lastname] FROM authors FOR XML PATH <row xmlns:au="urn:authors" au:authorid=" "> Bob Smith...

Producing an XML Schema with FOR XML XMLSCHEMA keyword prepends XML Schema to FOR XML can be used with other keywords schema namespace can be specified xml = (SELECT * FROM authors FOR XML AUTO, TYPE, XMLSCHEMA('urn:authors')

More FOR XML Enhancements Many refinements and extensions to FOR XML element-centric XML using FOR XML RAW generate xsi:nil for NULL database values FOR XML AUTO/RAW, ELEMENTS XSINIL nested FOR XML queries can specify ROOT element can name element in FOR XML RAW changes to nesting algorithm in XML AUTO supports new data types varchar(max), nvarchar(max), varbinary(max) UDTs (must be cast/converted to XML in query)

Using XML Data Type with OpenXml OpenXml function can use XML data type originally used varchar or TEXT input still must be parsed with sp_xml_preparedocument permits user-defined fill into existing tables overflow column may be xml -- this assumes that the document -- looks exactly like the table CREATE PROCEDURE xml) AS int exec SELECT * FROM '/ROOT',2) WITH (mytable) AS A INTO mytable exec

Inserting XML Through Bulk Load New BULK Rowset Provider can insert XML inserts from file to XML column using SINGLE_BLOB option inserts only XML column SINGLE_CLOB, SINGLE_NCLOB also work SINGLE_BLOB avoids encoding problems format file required if inserting more than one row CREATE TABLE invoices ( rowid int primary key identity, invoice xml ) INSERT invoices SELECT * FROM OPENROWSET (BULK 'c:\invoice.txt', SINGLE_BLOB) as X

XML data type and FOR XML enhancements

SQL Server 2005 Support of XQuery XQuery is supported through methods on the XML type xml.exist - returns bool xml.value - returns scalar xml.query - returns XML data type instance xml.nodes - returns one column rowset w/XML column xml.modify - modifies an instance These methods can return XML data type (query) or SQL Scalar (exist, value) columns in rowsets - when used with SQL SELECT variables

XQuery Expressions XQuery is a superset of XPath can use XPath or FLWOR expressions almost all valid XPath statements are also XQuery statements XPath used to select values in FLWOR expressions for $p in /people/person where $p/age > 30 order by $p/age[1] return $p/name

FLWOR Expressions XML Queries are FLWOR expressions made up of five types of clause FOR LET (not supported by SQL Server 2005) WHERE ORDER BY RETURN

Constructors Constructors are used to construct nodes constructors can construct static content; XML Namespaces serialized form constructors can construct dynamic content dynamic content can be content of elements and/or attributes dynamic part of constructor enclosed in curly braces or use literal constructor

Martin Simon Dynamic constructors: Text return {$p/name[1]/givenName[1]/text()}

Literal constructors return element person { attribute name {data($p/name[1]/givenName[1]/text()[1])} }

value and exist XML data type methods value() data type method accesses scalar value in xml column XQuery or simple XPath can be used exist() data type method tests conditions in xml column XQuery or simple XPath can be used used with column name separated by "." select from Invoices... Invoices table invoice column XPath expression gets value attribute

xml.exist xml.exist uses XML type and XQuery expression returns false if query returns NULL sequence returns true otherwise Usable in XML type check constraints xml functions must be encapsulated as UDF

xml.value xml.value return a SQL Server scalar type SQL type, xml data type instance, XQuery as input returns scalar type or NULL cannot return XML type instance Usable within SQL Server in predicates as result values

Using xml.value -- insert some rows INSERT xml_tab VALUES(' ') INSERT xml_tab VALUES(' ') INSERT xml_tab VALUES(' ') -- this query SELECT id, AS name FROM xml_tab -- yields this resultset id name curly 2 larry 3 moe

xml.query xml.query returns an XML data type instance XML type instance and XQuery are inputs can return scalar type as XML fragment can use constructors to compose new data Soda Ice from invoice column in Invoices table SELECT invoice.query('Invoice/LineItem') FROM Invoices Soda Ice selects LineItem's from XML data -- yields this resultset Soda Ice

Using Relational Data in XQuery SQL Server XQuery can combine relational and XML sql:variable - uses TSQL variables sql:column - uses column value same row as XML type column

Using Relational Data With XML -- returns moe in record number x -- where x is the ID column, or blank column SELECT xml_col.query(' for $b in //person where return { } in record number {sql:column("xml_tab.id")} ') FROM xml_tab -- returns moe is a stooge VARCHAR(50) = ' is a stooge' SELECT xml_col.query(' for $b in //person where return { } } ') FROM xml_tab

nodes method nodes can decompose an XML data type similar to value(), but produces references to XML nodes many nodes rather than single scalar value result is one-column rowset T-SQL CROSS APPLY can be use to produce rowset similar to OpenXML better performance, XML column is already parsed -- xmlinvoices table contains custid, xmlinv columns -- return one row for each lineitem SELECT custid, tab.col.query('itemno') --get itemno subelement FROM xmlinvoices CROSS APPLY xmlinv.nodes('/invoice/lineitem') as tab(col) WHERE custid = 'ALFKI'

XQuery DML - Using xml.modify -- use modify to insert a subelement 'insert into /Invoice[1] ') -- or insert an attribute attribute status{"backorder"} into /Invoice[1] ') -- this deletes all LineItem elements /Invoice/LineItems/LineItem') -- change the value of the CustomerName element value of /Invoice[1]/CustomerName[1]/text()[1] with "John Smith" ')

XQuery

Why SQL Server and Web Services? Databases can deal with Web service clients directly Using TDS limits potential clients Web service check box requirement OLE DB, ODBC,.NET data provider required JDBC driver required free TDS - reduced functionality Web services from SQL Server HTTP, all clients have it XML, all clients support it

SQL Server Web Services descriptions SQL Server Web services configured in database just like other database features SQL Server Web service operations part of database assemblies, sprocs in database just like other database functionality SQL Server Web service security part of data base just like other database security SQL Server Web services fully described inside database just as database objects should be

SQL Server directly leverages HTTP Uses HTTP kernel protocol stack less layers, better scalability and thru-put requires WS2K3 or XP SP2 Can use SSL Inetinfo.exe not used

Web Service configuration Web service endpoint must be configured location, access, and security Web service operations must be configured specific operation names and/or ad hoc sql batches CREATE ENDPOINT for HTTP used to configure names endpoint before FOR SOAP configures endpoint after FOR SOAP configures soap operations CREATE ENDPOINT genled... AS HTTP (...) FOR SOAP (...) endpoint name endpoint configuration operations configuration

Endpoint authentication Basic authentication supported (w/Windows login) WS-Security supported w/SQL login (WS-I standard) but must specify an endpoint authentication type too Digest and integrated authentication supported integrated requires client in same or trusted domain Multiple authentication types supported Client must also be granted CONNECT ENDPOINT privilege Clear_port and ssl_port may be specified CREATE ENDPOINT genLed... AS HTTP (Authentication = (INTEGRATED), PORTS = (CLEAR), CLEAR_PORT = ) FOR SOAP CREATE ENDPOINT genLed... AS HTTP (Authentication = (BASIC), PORTS = (SSL)... ) FOR SOAP

Endpoint startup Endpoint must be in started state to accept request initial state can be started, stopped, disabled disabled state requires SQL Server restart to enable Endpoint state changed by using ALTER HTTP ENDPOINT CREATE ENDPOINT generalLedger STATE = STARTED AS HTTP (... ) FOR SOAP (... ) endpoint will accept requests CREATE ENDPOINT generalLedger STATE = STOPPED AS HTTP (... ) FOR SOAP (... ) endpoint will not accept requests

Operation configuration Operations supported must be configured operation name, function, schema defined after FOR SOAP Context for Web methods must be configured namespace, database used, etc. CREATE ENDPOINT genLed STATE = STARTED AS HTTP (... ) FOR SOAP ( WEBMETHOD a (...), WEBMETHOD b (...),... ) operations supported by endpoint context for operations

Endpoint operation Webmethod is used to define an endpoint operation namespace and name of operation name of stored proc or udf that implements it standard schema, none or default Multiple operations may be defined FOR SOAP WEBMETHOD ' (name='Pubs.dbo.GetCInfo', schema=STANDARD ), WEBMETHOD..., client sees a CustomerInfo operation operation is implemented by the GetCInfo sproc in the pubs database schema definition is available for this operation

Endpoint context Context operations execute in must be defined automatic WSDL generation ad hoc queries, disabled by default database used, default for login if not specified namespace for endpoint FOR SOAP ( WEBMETHOD..., WSDL = DEFAULT, BATCHES = ENABLED, DATABASE = 'pubs', NAMESPACE = 'urn:Accounting' )

Typical Web Service configuration CREATE ENDPOINT Accounting STATE = STARTED AS HTTP ( SITE = ' PATH = '/nwind', AUTHENTICATION = (INTEGRATED), PORTS = (CLEAR) FOR SOAP ( WEBMETHOD ' (name='Northwind.dbo.GetCustInfo', schema=STANDARD ), WSDL = DEFAULT, BATCHES = ENABLED, DATABASE = 'northwind', NAMESPACE = ' ) GO

Creating and calling a SQL Server 2005 web service

static void Main(string[] args) { // instantiate the proxy class myprog.localhost.soap s = new myprog.localhost.soap(); // use security of current client principal s.Credentials = System.Net.CredentialCache.DefaultCredentials; object[] oa; try { oa = s.AuthorsByRoyaltyAsDataSets(50); processResults(oa); } catch (Exception e) { Console.WriteLine("{0} {1}", e.GetType().ToString(), e.Message); if (oa) Console.WriteLine("error, {0} results", oa.Length); }.NET Client to Retrieve Results

void processResults(object oa[]) { for (int i=0;i<oa.Length;i++) { switch (oa[i].GetType().ToString()) { case "System.Data.DataSet": DataSet ds = (DataSet)oa[i]; Console.WriteLine(ds.GetXml()); break; case "myprog.localhost.SqlMessage": SqlMessage sqlm = (SqlMessage)oa[i]; Console.WriteLine("Error: {0} in proc {1}", sqlm.Message, sqlm.Procedure); break; case "System.Xml.XmlElement": XmlElement elem = (XmlElement)oa[i]; Console.WriteLine(elem.OuterXml); break; // more cases... default: Console.WriteLine("Unexpected type {0}:{1}", oa[i].GetType().ToString(), oa[i].ToString()); break; } Processing Results