This sample application shows how data in an XML documents can be used to update records in a database table. The application shows the process of:
The application assumes that the client has an XML document that is created using another application. The application uses OPENXML to shred the XML document and creates the rowset that is passed to UPDATE statement.
Before executing this example, you must create a virtual root. For more information, see Creating the nwind Virtual Directory.
The template executes a stored procedure (sp_update_employee). The XML document is passed to the stored procedure as a text parameter.
The stored procedure:
These are the steps to create a working sample:
CREATE TABLE Employee(eid int, fname varchar(20), lname varchar(20))
INSERT INTO Employee VALUES (1, 'Nancy', 'Davolio')
INSERT INTO Employee VALUES (2, 'Andrew', 'Fuller')
CREATE PROC sp_update_employee @empdata ntext
AS
DECLARE @hDoc int
exec sp_xml_preparedocument @hDoc OUTPUT,@empdata
UPDATE Employee
SET
Employee.fname = XMLEmployee.fname,
Employee.lname = XMLEmployee.lname
FROM OPENXML(@hDoc, '/root/Employee')
WITH Employee XMLEmployee
WHERE Employee.eid = XMLEmployee.eid
EXEC sp_xml_removedocument @hDoc
SELECT *
from Employee
FOR XML AUTO
<root xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<sql:header>
<sql:param name="empdata"><Employee/></sql:param>
</sql:header>
<sql:query>
exec sp_update_employee @empdata
</sql:query>
</root>
This is the HTML form:
<html>
<body>
<form name="Employee"
action="http://localhost/nwind/Template/UpdateEmployee.XML"
method="POST">
This app assumes that client has this simple
XML document created using some other application and you want to
update the tables based on the data in this document.<br>
---------------------------------------<br>
<input type=hidden name="contenttype" value="text/xml">
<textarea name="empdata" cols=50 rows=5>
<root>
<Employee eid="1" lname="Leverling" fname="Janet"/>
<Employee eid="2" lname="Peacock" fname="Margaret"/>
</root>
</textarea>
<br><input type=Submit value="Submit">
</form>
</body>
</html>
IIS Virtual Directory Management for SQL Server