Using XML in Stored Procedures

Now that you have seen several ways to produce XML documents from queries, it's time to take a look at how to deal with XML where you are most likely to use it: inside stored procedures. To talk to ActiveX Data Objects (ADO), URL queries, templates, and other data sources, your procedures need to know how to read and write XML. If you are not familiar with stored procedures, please refer to Chapter 28, "Creating and Managing Stored Procedures in SQL Server," before proceeding. Listing 41.10 provides the basis for our discussion.

Listing 41.10 A Stored Procedure That Reads XML Input and Writes XML Output
(@xml varchar(1000))
declare       @ixml int,
       @CustomerID nvarchar(5),
       @EmployeeID int

EXECUTE sp_xml_preparedocument @ixml OUTPUT, @xml

SELECT @CustomerID = CustomerID, @EmployeeID = EmployeeID
FROM OPENXML(@ixml, 'sp/row')
       CustomerID nvarchar(5) '@CustomerID',
       EmployeeID int '@EmployeeID'

SELECT Customer.CustomerID, OrderID, LastName + ', ' + FirstName as EmployeeName
FROM Customers Customer
JOIN Orders [Order] ON Customer.CustomerID = [Order].CustomerID
AND [Order].CustomerID = @CustomerID
JOIN Employees Employee ON [Order].EmployeeID = Employee.EmployeeID
AND [Order].EmployeeID = @EmployeeID

       return 0
       return 1
EXEC sp_xml_removedocument @ixml

In the CREATE PROCEDURE statement, you declare a single input parameter @xml. This is a convention I use based on a principle of simplicity. Size @xml according to the size of your expected input xml. Your XML input string should contain all the values that, prior to the advent of XML support, would have been found in a complex list of input parameters of varying datatypes.

Next, declare the actual parameters that the procedure will need as local variables. A select statement that uses the OPENXML extension will then populate them.

Listing 41.11 shows the execution of our example using Query Analyzer and its result.

Listing 41.11 The Result of a Call to S_ORDERS_BY_CUSTOMER_AND_EMPLOYEE_XML
        <row CustomerID=''ANTON'' EmployeeID=''7''/>


<Customer CustomerID="ANTON">
        <Order OrderID="10507" EmployeeName="King, Robert"/>
        <Order OrderID="10573" EmployeeName="King, Robert"/>

You should standardize the structure of your XML input by using a common root element name. Then, format its child elements so they look like they could have been produced by the FOR XML clause. This is because OPENXML works a lot like FOR XML in reverse. Following are some suggested conventions:

  • When your stored procedure needs input values that correspond to columns in multiple tables, create a flat input XML structure, resembling what FOR XML RAW might produce (as exemplified in Listing 41.10).

  • When input values can be logically mapped to the columns in a single table and are small in terms of character count (say, 100 or less), name the attributes after the column to which they correspond. Then, name the element that holds these attributes after the table to which they belong, such as <CUSTOMERS CUSTOMERID='ANTON'/>. This is known as attribute-centric data mapping and it resembles the output produced by FOR XML AUTO.

  • If input values are larger, store them in elements named after the column to which they correspond. (This is also safer in terms of data processing?XML elements are less restricted than attributes in what characters they can contain.) These elements should then be child elements of an element named after the table to which they belong, such as <CUSTOMERS><CUSTOMERID>ANTON</CUSTOMERID></CUSTOMERS>. This is known as element-centric mapping and it resembles the output produced by FOR XML AUTO, ELEMENTS.

Use these techniques to structure your XML in a logical manner and debugging will be easier because your XML (whenever possible) is both human and machine-readable. In addition, XML is input ready for easy use with OPENXML. The more your input XML resembles table structures, the simpler your OPENXML queries will be.

    Part III: SQL Server Administration
    Part IV: Transact-SQL
    Part V: SQL Server Internals and Performance Tuning
    Part VI: Additional SQL Server Features