<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SqlXml.org Main Feed</title><link>http://sqlxml.org</link><description>SqlXml.org provides developers with answers to frequently asked questions about SqlXml technologies.</description><generator>.Text Adapted Rss Generator</generator><item><title>HOW TO: Get the XML text of a FOR XML query within SQL.</title><link>http://sqlxml.org/faqs.aspx?faq=104</link><pubDate>Sat, 11 Sep 2004 18:52:32 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=104</guid><comments>http://sqlxml.org/faqs.aspx?faq=104#comment</comments><description>&lt;p&gt;&lt;b&gt;Is there a way to get the XML text of a FOR XML query?&lt;/b&gt;&lt;br&gt;
&lt;i&gt;Answer provided by &lt;a href="http://www.sommarskog.se/"&gt;Erland Sommarskog&lt;/a&gt;&lt;/i&gt;.&lt;br&gt;
Check Erland's website for more great SQL Server information.&lt;/p&gt;

&lt;p&gt; 
In SQL2000 you can do this:
&lt;/p&gt;

&lt;pre class="code"&gt;
   
create table xml (x ntext)
go
   
insert xml
SELECT * FROM OPENQUERY(MSDALOCAL, 
       'SELECT * FROM sysobjects FOR XML AUTO')
go
   
select * from xml
go

drop table xml

&lt;/pre&gt;

&lt;p&gt;MSDALOCAL is a linked server, which can be a loopback to your own server.
But, this is important, it has to be set up with MSDASQL - OLE DB over
ODBC. If you use SQLEOLDDB, which is the default, then you get binary
data instead.
&lt;/p&gt;

&lt;p&gt;
If you want the data in a variable, beware that you cannot declare ntext
variables in T-SQL, you would have to use nvarchar(4000) which may cause
truncation.
&lt;/p&gt;
&lt;p&gt;
In SQL2005 there is a xml datatype, all these sort of things are simpler.
&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>Using Metaproperties in OpenXML</title><link>http://sqlxml.org/faqs.aspx?faq=103</link><pubDate>Wed, 12 Nov 2003 18:35:17 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=103</guid><comments>http://sqlxml.org/faqs.aspx?faq=103#comment</comments><description>&lt;p&gt;Goal: Using the following XML, insert three node records in the node table and then three two subnode records into the subnode table with a reference to the correct parent node. The node table uses a GUID as the node key.&lt;/p&gt;
&lt;pre class="code"&gt;
&amp;nbsp;
&amp;lt;nodes&gt;
  &amp;lt;node attr="123"&gt;
    &amp;lt;subnode val="789" /&gt;
    &amp;lt;subnode val="456" /&gt;
  &amp;lt;/node&gt;
  &amp;lt;node attr="123" /&gt;
  &amp;lt;node attr="321" /&gt;
&amp;lt;/nodes&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;p&gt;
If the attr values were unique then we could do a simple join on two OpenXML tables. However, since the attr values are not unique this cannot be done. The following SQL code demonstrates how this can be done. The code will be explained below.
&lt;/p&gt;
&lt;pre class="code"&gt;
&amp;nbsp;
declare @i int
&amp;nbsp;
-- prepare the document
exec sp_xml_preparedocument @i output, '
&amp;lt;nodes&gt;
  &amp;lt;node attr="123"&gt;
    &amp;lt;subnode val="789" /&gt;
    &amp;lt;subnode val="456" /&gt;
  &amp;lt;/node&gt;
  &amp;lt;node attr="123" /&gt;
  &amp;lt;node attr="321" /&gt;
&amp;lt;/nodes&gt;'
&amp;nbsp;
-- create a table variable to store values
declare @t table(
 nodeid uniqueidentifier,
 oxid int, 
 attr int)
&amp;nbsp;
-- fill the table variable
insert into @t
select  newid(), id, attr
from  OpenXml(@i, 'nodes/node', 9) with (attr int, id int '@mp:id')
&amp;nbsp;
-- insert into our node table
-- insert into nodetable select nodeid, attr from @t
&amp;nbsp;
-- insert into our subnode table
-- insert into subnodetable
select  t.nodeid, ox.val
from  @t t,
 OpenXml(@i, 'nodes/node/subnode', 9) with (attr int '../@attr', val int, parentid int '@mp:parentid') ox
where t.oxid = ox.parentid
&amp;nbsp;
-- remove the xml document
exec sp_xml_removedocument @i
&amp;nbsp;
&lt;/pre&gt;
&lt;p&gt;
The code above makes use of the xml metaproperties that are available when using OpenXML. These properties are documented &lt;a href="http://msdn.microsoft.com/library/en-us/xmlsql/ac_openxml_4iwc.asp"&gt;here&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;
In the first select statement we get the node id value using the @mp:id which gives us the unique id for each node element. Then in the next query we can get the @mp:parentid of the subnode which points to the @mp:id of the node. This allows us to figure out which node the subnode belongs to without needing a unique value in the node itself.&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How To: Shape FOR XML AUTO results using SQL views</title><link>http://sqlxml.org/faqs.aspx?faq=102</link><pubDate>Tue, 04 Nov 2003 19:32:45 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=102</guid><comments>http://sqlxml.org/faqs.aspx?faq=102#comment</comments><description>&lt;P&gt;Sometimes it is easier to use FOR XML AUTO to retrieve XML from SQL Server than it is to use FOR XML EXPLICIT. However, if you're combining data from multiple tables you may not get results you expect using the AUTO mode.&lt;/P&gt;
&lt;P&gt;Suppose that we want to get a list of the products in our database, but we also want to include the supplier name and category name. Below is an example template that you can run against the Northwind database.&lt;/P&gt;&lt;PRE class=code&gt;&amp;nbsp;
&amp;lt;products&amp;gt;
 &amp;lt;sql:query xmlns:sql="urn:schemas-microsoft-com:xml-sql"&amp;gt;
select  ProductID,
 ProductName,
 CompanyName,
 CategoryName
from Products, Suppliers, Categories
where Products.SupplierID = Suppliers.SupplierID
 and Products.CategoryID = Categories.CategoryID
for xml auto
 &amp;lt;/sql:query&amp;gt;
&amp;lt;/products&amp;gt;
&amp;nbsp;
&lt;/PRE&gt;
&lt;P&gt;If you run the above template (or just run the select statement in query analyzer) you will get the right results, but the format leaves something to be desired. Below is a snippet of what the output will look like. &lt;/P&gt;&lt;PRE class=code&gt;&amp;nbsp;
&amp;lt;products&amp;gt;
 &amp;lt;Products ProductID="1" ProductName="Modified product"&amp;gt;
  &amp;lt;Suppliers CompanyName="Exotic Liquids"&amp;gt;
   &amp;lt;Categories CategoryName="Beverages"/&amp;gt;
  &amp;lt;/Suppliers&amp;gt;
 &amp;lt;/Products&amp;gt;
 &amp;lt;Products ProductID="2" ProductName="Chang"&amp;gt;
  &amp;lt;Suppliers CompanyName="Exotic Liquids"&amp;gt;
   &amp;lt;Categories CategoryName="Beverages"/&amp;gt;
  &amp;lt;/Suppliers&amp;gt;
 &amp;lt;/Products&amp;gt;
 ...
&amp;lt;/products&amp;gt;
&amp;nbsp;
&lt;/PRE&gt;
&lt;P&gt;All the data is there, but it isn't very readable and we are creating a lot of data bloat by adding the extra tags. We could use a FOR XML EXPLICIT query instead of the FOR XML AUTO or we could just use a SQL view. Below is the SQL statement to create our view. We just use the select statement with the FOR XML AUTO and create a view from it. &lt;/P&gt;&lt;PRE class=code&gt;&amp;nbsp;
create view vw_ProductInfo
as
&amp;nbsp;
select  ProductID,
 ProductName,
 CompanyName,
 CategoryName
from Products, Suppliers, Categories
where Products.SupplierID = Suppliers.SupplierID
 and Products.CategoryID = Categories.CategoryID
&amp;nbsp;
&lt;/PRE&gt;
&lt;P&gt;Once we have created our view we can modify our template. Now we will just select * from the view and add the FOR XML AUTO statement to it. &lt;/P&gt;&lt;PRE class=code&gt;&amp;nbsp;
&amp;lt;products&amp;gt;
 &amp;lt;sql:query xmlns:sql="urn:schemas-microsoft-com:xml-sql"&amp;gt;
select  *
from vw_ProductInfo as product
for xml auto &amp;lt;/sql:query&amp;gt;
&amp;lt;/products&amp;gt;
&amp;nbsp;
&lt;/PRE&gt;
&lt;P&gt;The "as product" will shape the resulting XML further since the elements will be named product instead of vw_ProductInfo. The resulting XML will be as follows.&lt;/P&gt;&lt;PRE class=code&gt;&amp;nbsp;
&amp;lt;products&amp;gt;
 &amp;lt;product ProductID="1" ProductName="Modified product" CompanyName="Exotic Liquids"  CategoryName="Beverages"/&amp;gt;
 &amp;lt;product ProductID="2" ProductName="Chang" CompanyName="Exotic Liquids" CategoryName="Beverages"/&amp;gt;
 ...
&amp;lt;/products&amp;gt;
&amp;nbsp;
&lt;/PRE&gt;
&lt;P&gt;So by using a SQL view the results of a FOR XML AUTO query can be shaped to the way we want it to look.&lt;/P&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How To: Use OpenXML on an Xml document with a default namespace</title><link>http://sqlxml.org/faqs.aspx?faq=101</link><pubDate>Fri, 31 Oct 2003 14:53:36 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=101</guid><comments>http://sqlxml.org/faqs.aspx?faq=101#comment</comments><description>&lt;P&gt;&lt;I&gt;Answer provided by Brian.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;Hopefully this will prevent another developer from working until 12:30 AM to solve a mystery. I looked for the answer to this one for 4 hours. None of the documentation, newsgroups or experts out there give a simple example of how to use sp_xml_preparedocument with an xml document that had a single default namespace. They cover namespaces where the tags are prefixed and where multiple namespaces exist. But they don't cover the simplest case. &lt;/P&gt;
&lt;P&gt;If you want lots of fun take the prefix off of the field names (change [a:name] to [name])and watch the query return NULL for the fields.&lt;/P&gt;&lt;PRE class=code&gt;&amp;nbsp;
/****** run the code below in sql query analizer ********/
&amp;nbsp;
DECLARE @hDoc int
&amp;nbsp;
EXEC sp_xml_preparedocument @hDoc OUTPUT, 
   '&amp;lt;request xmlns="http://www.mytest.com"&amp;gt;
     &amp;lt;table&amp;gt;
      &amp;lt;name&amp;gt;NameField&amp;lt;/name&amp;gt;
      &amp;lt;date&amp;gt;2003-06-14&amp;lt;/date&amp;gt;
      &amp;lt;time&amp;gt;10:15&amp;lt;/time&amp;gt;
     &amp;lt;/table&amp;gt;
    &amp;lt;/request&amp;gt;', 
  '&amp;lt;root xmlns:a="http://www.mytest.com"/&amp;gt;'
&amp;nbsp;
SELECT *
FROM OPENXML(@hDoc, '//a:table', 2)
WITH ([a:name] varchar(50), [a:date] varchar(50), [a:time] varchar(50))
&amp;nbsp;
EXEC sp_xml_removedocument @hDoc
&amp;nbsp;
&lt;/PRE&gt;
&lt;P&gt;The above code works by supplying the default namespace to the third parameter of the sp_xml_preparedocument stored procedure. Even though there is no prefix used for the default namespace, we still need to provide a prefix in order to reference the namespace.&lt;/P&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How To: Insert and Update with OpenXML</title><link>http://sqlxml.org/faqs.aspx?faq=100</link><pubDate>Tue, 28 Oct 2003 22:02:16 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=100</guid><comments>http://sqlxml.org/faqs.aspx?faq=100#comment</comments><description>&lt;P&gt;&lt;I&gt;I would like to update existing record if the ID exists in the table, otherwise insert the record(node) into the table.. &lt;/I&gt;&lt;/P&gt;
&lt;P&gt;This is pretty easy to do. Below is an example that uses a table named 'test' that has an ID column called xmlID and a data column called xmlData.&lt;/P&gt;&lt;PRE class=code&gt;&amp;nbsp;
declare @i int
&amp;nbsp;
exec sp_xml_preparedocument @i output, 
'&amp;lt;mydata&amp;gt;
  &amp;lt;test xmlID="3" xmlData="blah blah blah"/&amp;gt;
  &amp;lt;test xmlID="1" xmlData="blah"/&amp;gt;
&amp;lt;/mydata&amp;gt;'
&amp;nbsp;
insert into test 
select xmlID, xmlData 
from OpenXml(@i, 'mydata/test')
with (xmlID int, xmlData nvarchar(30))
where xmlID not in (select xmlID from test)
&amp;nbsp;
update test
set test.xmlData = ox.xmlData
from OpenXml(@i, 'mydata/test')
with (xmlID int, xmlData nvarchar(30)) ox
where test.xmlID = ox.xmlID
&amp;nbsp;
exec sp_xml_removedocument @i
&amp;nbsp;
&lt;/PRE&gt;
&lt;P&gt;So you can use the same openxml pointer to do an update and an insert.&lt;/P&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How To: Combine FOR XML AUTO queries using XSL</title><link>http://sqlxml.org/faqs.aspx?faq=99</link><pubDate>Thu, 23 Oct 2003 01:51:03 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=99</guid><comments>http://sqlxml.org/faqs.aspx?faq=99#comment</comments><description>&lt;P&gt;When constructing complex XML documents, many times the best solution is not to use FOR XML EXPLICIT but rather use a bunch of FOR XML AUTO queries and combine them with XSL. Below are a few examples of how to accomplish this.&lt;/P&gt;
&lt;P&gt;For these examples we will use the Northwind sample database.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose that Northwind needed to send a supplier the contact information for each customer that had bought their products (maybe due to a recall). Further, the supplier needed to have that XML in the format shown below:&lt;/P&gt;&lt;PRE class=code&gt;&amp;lt;Supplier SupplierID="1" Phone="(171) 555-2222"&amp;gt;
 &amp;lt;Contact Name="Charlotte Cooper" Title="Purchasing Manager"/&amp;gt;
 &amp;lt;Customers&amp;gt;
  &amp;lt;Customer CompanyName="Alfreds Futterkiste" Phone="030-0074321"&amp;gt;
   &amp;lt;Contact Name="Maria Anders" Title="Sales Represetative"/&amp;gt;
   &amp;lt;Products&amp;gt;
    &amp;lt;Product ProductID="1" ProductName="Modified product"/&amp;gt;
   &amp;lt;/Products&amp;gt;
  &amp;lt;/Customer&amp;gt;
 &amp;lt;/Customers&amp;gt;
&amp;lt;/Supplier&amp;gt;

&lt;/PRE&gt;
&lt;P&gt;Now you could create a FOR XML EXPLICIT query to do this, but it would be hard to read and hard to maintain because of the complexity of the XML document. So an alternative solution is to use simple FOR XML AUTO queries and then piece the information together using XSL. Below are the three queries we need to get all the data for our XML document.&lt;/P&gt;&lt;PRE class=code&gt;select SupplierID, Phone, ContactName, ContactTitle 
from Suppliers 
where SupplierID = 1
FOR XML AUTO
&amp;nbsp;
select ProductID, ProductName 
from Products 
where supplierID = 1
FOR XML AUTO
&amp;nbsp;
select  distinct c.CompanyName, Phone, ContactName, ContactTitle, p.ProductID 
from  Customers c, Orders o, [Order Details] od, Products p
where  c.CustomerID = o.CustomerID
 and o.OrderID = od.OrderID
 and p.ProductID = od.ProductID
 and p.SupplierID = 1
FOR XML AUTO

&lt;/PRE&gt;
&lt;P&gt;So how do we get all these selects as one resultset? The first option is to use XML Templates. This option works fairly well and can be used with either the SqlXml Managed classes or the IIS ISAPI. Another option would be to use .Net and execute three queries and then paste them together.&lt;/P&gt;
&lt;P&gt;First we'll look at a simple XML template that can be used to get these results and return them as a single document. Below is our template:&lt;/P&gt;&lt;PRE class=code&gt;&amp;lt;root xmlns:sql="urn:schemas-microsoft-com:xml-sql"&amp;gt;
 &amp;lt;sql:header&amp;gt;
   &amp;lt;sql:param name="SupplierID"/&amp;gt;
 &amp;lt;/sql:header&amp;gt;
 &amp;lt;sql:query&amp;gt;
  select SupplierID, Phone, ContactName, ContactTitle 
  from Suppliers 
  where SupplierID = @SupplierID
  FOR XML AUTO
 &amp;lt;/sql:query&amp;gt;
 &amp;lt;Products&amp;gt;
  &amp;lt;sql:query&amp;gt;
   select ProductID, ProductName 
   from Products 
   where supplierID = @SupplierID
   FOR XML AUTO
  &amp;lt;/sql:query&amp;gt;
 &amp;lt;/Products&amp;gt;
 &amp;lt;Customers&amp;gt;
  &amp;lt;sql:query&amp;gt;
   select distinct c.CompanyName, Phone, ContactName, ContactTitle, p.ProductID 
   from Customers c, Orders o, [Order Details] od, Products p
   where c.CustomerID = o.CustomerID
    and o.OrderID = od.OrderID
    and p.ProductID = od.ProductID
    and p.SupplierID = @SupplierID
   FOR XML AUTO
  &amp;lt;/sql:query&amp;gt;
 &amp;lt;/Customers&amp;gt;
&amp;lt;/root&amp;gt;

&lt;/PRE&gt;
&lt;P&gt;If we put this file in the virtual directory and create a mapping to it called supplier.xml we will get the following results when we execute the template (your results may vary slightly due to the data). I've also removed most of the customers to save bandwidth.&lt;/P&gt;&lt;PRE class=code&gt;&amp;lt;root xmlns:sql="urn:schemas-microsoft-com:xml-sql"&amp;gt;
 
 &amp;lt;Suppliers SupplierID="1" Phone="(171) 555-2222" ContactName="Charlotte Cooper" ContactTitle="Purchasing Manager"/&amp;gt;
 &amp;lt;Products&amp;gt;
  &amp;lt;Products ProductID="1" ProductName="Modified product"/&amp;gt;
  &amp;lt;Products ProductID="2" ProductName="Chang"/&amp;gt;
  &amp;lt;Products ProductID="3" ProductName="Aniseed Syrup"/&amp;gt;
 &amp;lt;/Products&amp;gt;
 &amp;lt;Customers&amp;gt;
  &amp;lt;c CompanyName="Alfreds Futterkiste" Phone="030-0074321" ContactName="Maria Anders" ContactTitle="Sales Representative"&amp;gt;
    &amp;lt;p ProductID="3"/&amp;gt;
  &amp;lt;/c&amp;gt;&amp;lt;c CompanyName="Antonio Moreno Taquería" Phone="(5) 555-3932" ContactName="Antonio Moreno" ContactTitle="Owner"&amp;gt;
    &amp;lt;p ProductID="2"/&amp;gt;
  &amp;lt;/c&amp;gt;
  ...
 &amp;lt;/Customers&amp;gt;
&amp;lt;/root&amp;gt;
&lt;/PRE&gt;
&lt;P&gt;Now that we have our XML results we can write the XSL to combine these and create our XML document. Below is an example of how you can do this with XSL. The XSL below will create the structure required.&lt;/P&gt;&lt;PRE class=code&gt;&amp;lt;?xml version="1.0" encoding="windows-1252"?&amp;gt;
&amp;lt;xsl:stylesheet 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        version="1.0"&amp;gt;
&amp;lt;xsl:template match="/"&amp;gt;
&amp;lt;Supplier&amp;gt;
  &amp;lt;xsl:attribute name="SupplierID"&amp;gt;
    &amp;lt;xsl:value-of select="root/Suppliers/@SupplierID"/&amp;gt;
  &amp;lt;/xsl:attribute&amp;gt;
  &amp;lt;xsl:attribute name="Phone"&amp;gt;
    &amp;lt;xsl:value-of select="root/Suppliers/@Phone"/&amp;gt;
  &amp;lt;/xsl:attribute&amp;gt;
  &amp;lt;xsl:apply-templates select="root/Suppliers"/&amp;gt;
  &amp;lt;Customers&amp;gt;
   &amp;lt;xsl:for-each select="root/Customers/c"&amp;gt;
    &amp;lt;Customer&amp;gt;
     &amp;lt;xsl:attribute name="CompanyName"&amp;gt;
      &amp;lt;xsl:value-of select="@CompanyName"/&amp;gt;
     &amp;lt;/xsl:attribute&amp;gt;
     &amp;lt;xsl:attribute name="Phone"&amp;gt;
      &amp;lt;xsl:value-of select="@Phone"/&amp;gt;
     &amp;lt;/xsl:attribute&amp;gt;
     &amp;lt;xsl:apply-templates select="."/&amp;gt;
     &amp;lt;Products&amp;gt;
     &amp;lt;xsl:for-each select="p"&amp;gt;
       &amp;lt;Product&amp;gt;
        &amp;lt;xsl:attribute name="ProductID"&amp;gt;
         &amp;lt;xsl:value-of select="@ProductID"/&amp;gt;
        &amp;lt;/xsl:attribute&amp;gt;
        &amp;lt;xsl:attribute name="ProductName"&amp;gt;
         &amp;lt;xsl:value-of 
select="../../../Products/Products[@ProductID=@ProductID]/@ProductName"/&amp;gt;
        &amp;lt;/xsl:attribute&amp;gt;
       &amp;lt;/Product&amp;gt;
     &amp;lt;/xsl:for-each&amp;gt;
     &amp;lt;/Products&amp;gt;
    &amp;lt;/Customer&amp;gt;
   &amp;lt;/xsl:for-each&amp;gt;
  &amp;lt;/Customers&amp;gt;
&amp;lt;/Supplier&amp;gt;
&amp;lt;/xsl:template&amp;gt;
&amp;lt;xsl:template match="Suppliers | c"&amp;gt;
  &amp;lt;Contact&amp;gt;
    &amp;lt;xsl:attribute name="Name"&amp;gt;
     &amp;lt;xsl:value-of select="@ContactName"/&amp;gt;
    &amp;lt;/xsl:attribute&amp;gt;
    &amp;lt;xsl:attribute name="Title"&amp;gt;
     &amp;lt;xsl:value-of select="@ContactTitle"/&amp;gt;
    &amp;lt;/xsl:attribute&amp;gt;
  &amp;lt;/Contact&amp;gt;
&amp;lt;/xsl:template&amp;gt;
  
&amp;lt;/xsl:stylesheet&amp;gt;
&lt;/PRE&gt;
&lt;P&gt;Once we have the XSL done we can save it and then set the sql:xsl attribute in our template to point to it. The result will now be transformed and will be in the format we want.&lt;/P&gt;
&lt;P&gt;A few things to take note of: 
&lt;OL&gt;
&lt;LI&gt;When using the IIS virtual directory to run this template, your XSL file must be accessible. To check this type the URL to your XSL file and it should come up. I generally create one template mapping to a folder and put all my templates and XSL files in that folder.&lt;/LI&gt;
&lt;LI&gt;When using the SqlXml Managed Classes you will need to set the BasePath property of the SqlCommand object to point to the folder where your XSL file is. &lt;/LI&gt;&lt;/OL&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How To: Using XML BulkLoading with .Net Streams</title><link>http://sqlxml.org/faqs.aspx?faq=98</link><pubDate>Mon, 20 Oct 2003 23:59:31 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=98</guid><comments>http://sqlxml.org/faqs.aspx?faq=98#comment</comments><description>&lt;P class=faqp&gt;&lt;I&gt;Answer provided by Craig Pearson&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;Here is an example of how to use XML BulkLoading with .Net Streams. &lt;/P&gt;&lt;PRE class=code&gt;using System.Runtime.InteropServices;
using SQLXMLBULKLOADLib;

[DllImport("OLE32.DLL", EntryPoint="CreateStreamOnHGlobal")]
extern public static int CreateStreamOnHGlobal( int hGlobalMemHandle, bool
fDeleteOnRelease, out UCOMIStream pOutStm);

public static void ExecuteBulkLoad(string connString, string schemaFile, 
                                          string userName, System.IO.Stream stream) 
{

    int i = System.Convert.ToInt32(stream.Length);
    byte[] dataBytes = new byte[i];
    stream.Position = 0;

    // declare the COM stream
    UCOMIStream data;

    // Create the stream (with no initial memory allocated)
    CreateStreamOnHGlobal(0, true, out data);
    int count = stream.Read(dataBytes, 0, i);
    data.Write(dataBytes, count, System.IntPtr.Zero);
    data.SetSize(i);

    // Create a bulkload instance as an single threaded apartment.
    System.Threading.Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA;
    SQLXMLBulkLoad3Class bulkLoad = new SQLXMLBulkLoad3Class();

    try {
        bulkLoad.ConnectionString = connString;
        bulkLoad.BulkLoad = true;
        bulkLoad.KeepIdentity = false;
        bulkLoad.XMLFragment = true;
        bulkLoad.Execute(schemaFile, data);
    }
    catch {
        throw;
    }
    finally {
        // close of the writer and the stream objects.
        if(stream !=null)
            stream.Close();

        bulkLoad = null;
    }
}

&lt;/PRE&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How do you setup SqlXml 3.0 on Windows Server 2003?</title><link>http://sqlxml.org/faqs.aspx?faq=97</link><pubDate>Fri, 13 Jun 2003 07:26:30 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=97</guid><comments>http://sqlxml.org/faqs.aspx?faq=97#comment</comments><description>&lt;p class="faqp"&gt;
This question comes up quite often in the newsgroups. Here are the steps to getting this setup.
&lt;/p&gt;

&lt;p&gt;
&lt;ol&gt;
&lt;li&gt;Download SqlXml 3.0 (this test was done with Sp2 Beta 1).&lt;/li&gt;
&lt;li&gt;Run the installation program. You will need to also install the SOAP toolkit 
if you want to use the SqlXml SOAP features.&lt;/li&gt;
&lt;li&gt;Open the IIS Virtual Directory Management tool under Start -&gt; Programs -&gt; SqlXml 3.0 -&gt; Configure IIS.&lt;/li&gt;
&lt;li&gt;Create a new virtual directory. Enter the information on the General tab and the Security tab. When you go to the Data Source tab you should be able to browse the databases on the server. If you can't then you need to check your security settings.&lt;/li&gt;
&lt;li&gt;Turn on Allow sql= queries so that you can test your setup. Make sure you turn this off when you are done testing.&lt;/li&gt;
&lt;/ol&gt;
&lt;/p&gt;

&lt;p&gt;If you're using SqlXml 3.0 Sp2 Beta 1 then your server should be working at this point. The easiest way to test this is to open the following URL (assuming the server is localhost and the virtual directory is Northwind).&lt;br/&gt;
&lt;a href="http://localhost/Northwind?sql=select%20@@version"&gt;http://localhost/Northwind?sql=select%20@@version&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;If you're not using sp2, then you will have to enable the SqlXml ISAPI Extension. To enable this follow these steps:
&lt;/p&gt;

&lt;p&gt;
&lt;ol&gt;
&lt;li&gt;Open the IIS Management tool (not the SqlXml one).&lt;/li&gt;
&lt;li&gt;Click on the Web Service Extension folder under your server.&lt;/li&gt;
&lt;li&gt;If you don't see SqlXml 3.0 listed, click Add a new Web service extension.&lt;/li&gt;
&lt;li&gt;Put SqlXml 3.0 as the description and then add the SqlXml ISAPI which is found at:
c:\Program Files\Common Files\System\Ole DB\sqlis3.dll (assuming default settings).
&lt;/li&gt;
&lt;/ol&gt;
Run the test again and it should now work.
&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>Why do I get a compilation error when importing the Microsoft.Data.SqlXml namespace?</title><link>http://sqlxml.org/faqs.aspx?faq=96</link><pubDate>Fri, 14 Mar 2003 19:21:49 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=96</guid><comments>http://sqlxml.org/faqs.aspx?faq=96#comment</comments><description>&lt;p class="faqp"&gt;
This is a pretty common question that many ASP.Net developers run into. If you want to use the SqlXml managed classes in ASP.Net &lt;i&gt;without&lt;/i&gt; using the code-behind feature, you will probably run into this problem. For our example we will use a very simply ASP.Net page.
&lt;/p&gt;

&lt;pre class="code"&gt;

&amp;lt;%@ Page Language="C#" Trace="false"%&gt;
&amp;lt;%@ Import namespace="Microsoft.Data.SqlXml" %&gt;

&amp;lt;script runat="server"&gt;
public void Page_Load()
{
	SqlXmlCommand command = new SqlXmlCommand("bogus");
}

&amp;lt;/script&gt;

&lt;/pre&gt;

&lt;p class="faqp"&gt;
If you create this ASP.Net page, add it to your site, and then access it, you should get a complilation error:
&lt;/p&gt;

&lt;pre class="code"&gt;
CS0234: The type or namespace name 'Data' does not exist in the class or 
namespace 'Microsoft' (are you missing an assembly reference?)
&lt;/pre&gt;

&lt;p class="faqp"&gt;
The simpliest fix to this problem is to just copy the Microsoft.Data.SqlXml.dll file from the Program Files\SQLXML 3.0\bin\ folder to the bin folder of your project. Once you do this the project should be able to find the reference. You should also be able to add a reference when you compile it.
&lt;/p&gt;
</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How can I use aggregates and the group by clause with FOR XML?</title><link>http://sqlxml.org/faqs.aspx?faq=95</link><pubDate>Fri, 14 Mar 2003 16:57:06 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=95</guid><comments>http://sqlxml.org/faqs.aspx?faq=95#comment</comments><description>&lt;p class="faqp"&gt;&lt;i&gt;Is there anyway I could return an XML with a SUM/GROUP BY clause in the
select?!?&lt;/i&gt;&lt;/p&gt;

&lt;p class="faqp"&gt;This is actually fairly simple to implement. For this example I will use the pubs database.&lt;/p&gt;

&lt;p class="faqp"&gt;The first step is to create the query you want to use. For our example we will count how many titles each author has.&lt;/p&gt;

&lt;pre class="code"&gt;

select 	count(*) titles, au_id
from 	titleauthor
group by au_id

&lt;/pre&gt;

&lt;p class="faqp"&gt;
If we were to add a FOR XML AUTO statement to the end of this code we would get an error. So instead we turn this statement into a view.
&lt;/p&gt;

&lt;pre class="code"&gt;

create view TitleCount
as
select 	count(*) titles, au_id
from 	titleauthor
group by au_id

&lt;/pre&gt;

&lt;p class="faqp"&gt;
Now we can use this view and create XML from it as follows.
&lt;/p&gt;

&lt;pre class="code"&gt;

select * from titlecount
for xml auto

&lt;/pre&gt;


</description><dc:creator>Bryant Likes</dc:creator></item><item><title>Why do get an error "Incorrect syntax near the keyword 'WITH'"?</title><link>http://sqlxml.org/faqs.aspx?faq=94</link><pubDate>Fri, 15 Nov 2002 22:11:12 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=94</guid><comments>http://sqlxml.org/faqs.aspx?faq=94#comment</comments><description>&lt;p class="faqp"&gt;
&lt;i&gt;Thanks to Kelley Smoot for suggesting this FAQ!&lt;/i&gt;
&lt;/p&gt;

&lt;p class="faqp"&gt;
This problem can occur for two reasons:
&lt;ol&gt;
&lt;li&gt;Your server is not running Microsoft SQL Server 2000.&lt;/li&gt;
&lt;li&gt;Your server is running 2000, but is running in compatability mode (6.5 or 7.0).&lt;/li&gt;
&lt;/ol&gt;
&lt;/p&gt;

&lt;p class="faqp"&gt;
If you're not running SQL 2000 then you can't use the XML features. To solve the compatability problem you have to switch to running in SQL Server 2000 mode. Check with your database administrator before changing this since there may be good reasons for it.
&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How To: Passing NTLM credentials when using ServerXMLHTTP.</title><link>http://sqlxml.org/faqs.aspx?faq=93</link><pubDate>Thu, 22 Aug 2002 18:28:30 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=93</guid><comments>http://sqlxml.org/faqs.aspx?faq=93#comment</comments><description>&lt;p class="faqp"&gt;
&lt;i&gt;Answer provided by Steven Sulzer&lt;/i&gt;
&lt;/p&gt;

&lt;p class="faqp"&gt;
Suppose you do not have a proxy server, and want NTLM credentials to be sent automatically to all servers, then try the following:
&lt;/p&gt;

&lt;pre class="code"&gt;

proxycfg -d -p " " "*"

&lt;/pre&gt;

&lt;p class&gt;
i.e, a blank (one space) name for the proxy server. And "*" for the bypass
list means bypass the proxy for all sites. Or you could just give the name of the
particular target server in the bypass list if that's all you care about.
&lt;/p&gt;

</description><dc:creator>Bryant Likes</dc:creator></item><item><title>Why does my SQLXML 3.0 setup fail when I have MDAC 2.7?</title><link>http://sqlxml.org/faqs.aspx?faq=92</link><pubDate>Thu, 15 Aug 2002 03:42:46 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=92</guid><comments>http://sqlxml.org/faqs.aspx?faq=92#comment</comments><description>&lt;p class="faqp"&gt;
&lt;i&gt;Answer provided by Amar Nalla.&lt;/i&gt;
&lt;/p&gt;

&lt;p class="faqp"&gt;
The SQLXML 3.0 install may fail on machines that have MDAC 2.7 installed on them. If this happens you can check the value for the following registry key:
&lt;/p&gt;

&lt;pre class="code"&gt;

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DataAccess\FullInstallVer

&lt;/pre&gt;

&lt;p class="faqp"&gt;
It should be set to the version of MDAC that you have installed. You can set this value and the install should run normally. 
&lt;/p&gt;

&lt;p class="faqp"&gt;
NOTE: Please edit the registry with care and only if you understand what you're doing.
&lt;/p&gt;
</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How To: Get the details of an exception with the SQLXML Managed Classes.</title><link>http://sqlxml.org/faqs.aspx?faq=91</link><pubDate>Fri, 09 Aug 2002 18:26:51 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=91</guid><comments>http://sqlxml.org/faqs.aspx?faq=91#comment</comments><description>&lt;p class="faqp"&gt;
A common problem when working with the .Net SQLXML Managed Classes is not being able to get the details of the exceptions when they occur. In order to get the details you will need to use a try-catch block of code. Below is an example of this.
&lt;/p&gt;

&lt;pre class="code"&gt;
[C#]
...
try
{
   // Perform your execute here
   cmd.ExecuteToStream(strm);
}
catch(SqlXmlException sxe)
{

   //in case of an error, this prints error returned.
   Console.WriteLine(sxe.ToString());
}
...
&lt;/pre&gt;

&lt;p class="faqp"&gt;
The code above writes the error to the console. If you're using ASP.Net you could also do something like the following.
&lt;/p&gt;

&lt;pre class="code"&gt;

Response.Write(sxe.ToString());

&lt;/pre&gt;

&lt;p class="faqp"&gt;
You can also throw a new exception as shown by the example posted by Oleg Chetverikov.
&lt;/p&gt;

&lt;pre class="code"&gt;

throw new Exception(sxe.ToString());

&lt;/pre&gt;

&lt;p class="faqp"&gt;
The important thing is that you get the details of the exception so that you can debug the problem.
&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>Why doesn't bulk loading work in .Net?</title><link>http://sqlxml.org/faqs.aspx?faq=90</link><pubDate>Tue, 30 Jul 2002 23:32:26 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=90</guid><comments>http://sqlxml.org/faqs.aspx?faq=90#comment</comments><description>&lt;P&gt;&lt;I&gt;Answer provided by weipingle&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;Bulkload works well in VB since VB uses single thread apartment by default. In C#, the same code will cause the following exception: &lt;/P&gt;
&lt;PRE&gt;
&amp;nbsp;
System.InvalidCastException: QueryInterface for interface
SQLXMLBULKLOADLib.ISQLXMLBulkLoad failed
&amp;nbsp;
&lt;/PRE&gt;
&lt;P&gt;In order to make bulkload work in C#, you need to make sure that the thread mode is STA by : &lt;/P&gt;
&lt;PRE class=code&gt;
&amp;nbsp;
System.Threading.Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA;
&amp;nbsp;
&lt;/PRE&gt;
&lt;P&gt;The bulkload code is simple: &lt;/P&gt;
&lt;PRE class=code&gt;
&amp;nbsp;
SQLXMLBULKLOADLib.SQLXMLBulkLoad3Class objXBL = new SQLXMLBULKLOADLib.SQLXMLBulkLoad3Class();
&amp;nbsp;
objXBL.ConnectionString = "Provider=sqloledb;server=server;database=db;uid=id;pwd=password";
&amp;nbsp;
objXBL.ErrorLogFile = "SQLXML3Books.errlog";
&amp;nbsp;
objXBL.KeepIdentity = false;
&amp;nbsp;
objXBL.Execute("Books.xsd", "Books1.xml");
&amp;nbsp;
&lt;/PRE&gt;

&lt;p&gt;The following code was provided by Gjalt Wijma which demonstrates how to use threading with bulk loading.&lt;/p&gt;

&lt;pre class="code"&gt;
&amp;nbsp;
// Create a new thread 
Thread bulkLoad = new Thread( new ThreadStart( LoadData ) ); 
bulkLoad.ApartmentState = ApartmentState.STA; 
bulkLoad.Start(); 
&amp;nbsp;
// Load data 
public void LoadData() 
{ 
    // Create new bulk load object SQLXMLBulkLoad3Class 
    objXBL = new SQLXMLBulkLoad3Class(); 
    // Set connection string 
    objXBL.ConnectionString = "..." 
    // Set log file 
    objXBL.ErrorLogFile = ".." 
    // Set keep identity to false 
    objXBL.KeepIdentity = false; 
    // Execute bulk load 
    objXBL.Execute("Books.xsd", "Books1.xml"); 
} 
&amp;nbsp;
&lt;/pre&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How can I get an element with a CDATA section?</title><link>http://sqlxml.org/faqs.aspx?faq=89</link><pubDate>Fri, 19 Jul 2002 18:15:47 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=89</guid><comments>http://sqlxml.org/faqs.aspx?faq=89#comment</comments><description>&lt;p class="faqp"&gt;
Using FOR XML EXPLICIT allows you to generate CDATA sections using the CDATA directive. However, many time you may want to have an element that contains the CDATA. To do this you can use the following example.
&lt;/p&gt;

&lt;pre class="code"&gt;

select  1 as Tag,
 null as Parent,
 employeeID as [emp!1!E_ID!element],
 '&amp;lt;![CDATA[' + firstname + ']]&gt;' as [emp!1!E_name!xml]
from  employees
for xml explicit

&lt;/pre&gt;

&lt;p class="faqp"&gt;
By using the XML directive in combination with the added text you get the desired result.
&lt;/p&gt;
</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How can I create recursive XML?</title><link>http://sqlxml.org/faqs.aspx?faq=88</link><pubDate>Mon, 29 Apr 2002 08:01:10 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=88</guid><comments>http://sqlxml.org/faqs.aspx?faq=88#comment</comments><description>&lt;p&gt;You can do this several ways:&lt;br/&gt;
With XSL: &lt;a href="http://sqlxml.org/faqs.aspx?55"&gt;http://sqlxml.org/faqs.aspx?55&lt;/a&gt;&lt;br/&gt;
With XSD: &lt;a href="http://sqlxml.org/faqs.aspx?63"&gt;http://sqlxml.org/faqs.aspx?63&lt;/a&gt;&lt;br/&gt;
With SQL: &lt;a href="http://sqlxml.org/faqs.aspx?53"&gt;http://sqlxml.org/faqs.aspx?53&lt;/a&gt;
&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>Tutorial: Using SQLXML Templates with ASP.Net</title><link>http://sqlxml.org/faqs.aspx?faq=87</link><pubDate>Mon, 29 Apr 2002 07:46:02 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=87</guid><comments>http://sqlxml.org/faqs.aspx?faq=87#comment</comments><description>&lt;img src="/images/devhood.gif"/&gt;

&lt;p&gt;
I wrote this as a tutorial for the &lt;a href="http://devhood.com"&gt;DevHood&lt;/a&gt; website. The site is a developer website for students. Click the link below to view the tutorial.
&lt;/p&gt;

&lt;p&gt;
&lt;a href="http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=424"&gt;Using SQLXML Templates with ASP.Net&lt;/a&gt;
&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How can return XML from a stored procedure using VB?</title><link>http://sqlxml.org/faqs.aspx?faq=86</link><pubDate>Wed, 24 Apr 2002 04:38:44 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=86</guid><comments>http://sqlxml.org/faqs.aspx?faq=86#comment</comments><description>&lt;p class="faqp"&gt;
I've gotten quite a few requested for an example using a stored procedure instead of a template. So this example uses a regular stored procedure.
&lt;ol&gt;
&lt;li&gt;Create a new EXE project in VB.

&lt;li&gt;Add references to MSXML 4.0 and ADO 2.6

&lt;li&gt;Create the stored procedure listed below.&lt;/li&gt;

&lt;li&gt;Copy the code below into your form's code. Be sure to change the connection string.
&lt;/ol&gt;
&lt;/p&gt;

&lt;p class="faqp"&gt;Stored Procedure&lt;/p&gt;

&lt;pre class="code"&gt;

create proc employee_get
(
	@id int
)
as

	select 	FirstName, 
		LastName, 
		Title,
		Region
	from 	employees
	where	employeeid = @id
	for xml auto

go

&lt;/pre&gt;

&lt;p class="faqp"&gt;VB Code&lt;/p&gt;

&lt;pre class="code"&gt;

Dim oCmd As Command
Dim oPrm As Parameter
Dim oDom As IXMLDOMDocument2
   
Set oDom = New DOMDocument40
   
Set oCmd = New Command
oCmd.ActiveConnection = "Provider=SQLOLEDB; Data Source=&lt;your server&gt;; " &amp; _
    "User ID=&lt;your user&gt;; Password=&lt;your pass&gt;; Database=Northwind"
       
oCmd.CommandText = "employee_get"
oCmd.CommandType = adCmdStoredProc
    
Set oPrm = New Parameter
oPrm.Name = "@id"
oPrm.Value = "1"
oPrm.Type = adInteger
oPrm.Size = 4
oCmd.Parameters.Append oPrm
        
oCmd.Properties("Output Stream") = oDom
oCmd.Execute , , 1024

oDom.Save "c:\temp\results.xml"
    
Unload Me

&lt;/pre&gt;

&lt;p class="faqp"&gt;
&lt;b&gt;Other Resources:&lt;/b&gt;&lt;br/&gt;
&lt;a href="http://sqlxml.org/faqs.aspx?1"&gt;Returning XML in VB with a template&lt;/a&gt;&lt;br/&gt;
&lt;img src="/images/orange.gif" style="visibility:none;" vspace="2" height="1" width="1"/&gt;&lt;br/&gt;
&lt;a href="http://support.microsoft.com/support/kb/articles/Q271/6/20.ASP"&gt;HOWTO: Retrieve XML Data by Using a SQL XML Query in a Visual Basic Client&lt;/a&gt;&lt;br/&gt;
&lt;img src="/images/orange.gif" style="visibility:none;" vspace="2" height="1" width="1"/&gt;&lt;br/&gt;
&lt;a href="http://support.microsoft.com/support/kb/articles/Q271/6/21.ASP"&gt;HOWTO: Retrieve XML Data with a Template File from a Visual Basic Client&lt;/a&gt;&lt;br/&gt;
&lt;/p&gt;

&lt;br&gt;
</description><dc:creator>Bryant Likes</dc:creator></item><item><title>Why do I get a "queries not allowed" error?</title><link>http://sqlxml.org/faqs.aspx?faq=85</link><pubDate>Tue, 23 Apr 2002 23:03:37 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=85</guid><comments>http://sqlxml.org/faqs.aspx?faq=85#comment</comments><description>&lt;p class="faqp"&gt;
Check the box that says "Allow URL queries" in the Virtual Directory
Management tool.
&lt;/p&gt;

&lt;p class="faqp"&gt;
&lt;a href="http://msdn.microsoft.com/library/en-us/iisadmin/iisadmin_50oo.asp"&gt;Virtual Directory Properties Dialog Box (Settings Tab)&lt;/a&gt;
&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How can I insert parent-child foreign keys that are autogenerated?</title><link>http://sqlxml.org/faqs.aspx?faq=84</link><pubDate>Tue, 23 Apr 2002 22:57:53 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=84</guid><comments>http://sqlxml.org/faqs.aspx?faq=84#comment</comments><description>&lt;p class="faqp"&gt;
&lt;i&gt;Answer provided by Avner Aharoni.&lt;/i&gt;
&lt;/p&gt;

&lt;p class="faqp"&gt;
This is not currently supported by SQLXML 3.0. It should be supported in the next version of SQLXML.
&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How can I prevent special characters from being encoded?</title><link>http://sqlxml.org/faqs.aspx?faq=83</link><pubDate>Tue, 23 Apr 2002 22:51:54 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=83</guid><comments>http://sqlxml.org/faqs.aspx?faq=83#comment</comments><description>&lt;p&gt;There are several directives that you can use with FOR XML (see &lt;a href="http://msdn.microsoft.com/library/en-us/xmlsql/ac_openxml_4y91.asp"&gt;BOL&lt;/a&gt; for complete list). 
&lt;p&gt;Below is a short for xml explicit query that shows how content can be either encoded or not encoded.&lt;/p&gt;
&lt;pre class="code"&gt;
&amp;nbsp;
select 1 as Tag, null as Parent,
 '&amp;lt;hi&gt;' as [example!1!notencoded!xml],
 '&amp;lt;hi&gt;' as [example!1!encoded!element]
for xml explicit
&amp;nbsp;
&lt;/pre&gt;
&lt;p&gt;
The results of this query are:
&lt;/p&gt;
&lt;pre class="code"&gt;
&amp;nbsp;
&amp;lt;example&gt;&amp;lt;notencoded&gt;&amp;lt;hi&gt;&amp;lt;/notencoded&gt;&amp;lt;encoded&gt;&amp;amp;lt;hi&amp;amp;gt;&amp;lt;/encoded&gt;&amp;lt;/example&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;p&gt;
Notice how the encoded results have been encoded. 
&lt;/p&gt;
&lt;p&gt;
&lt;b&gt;&lt;i&gt;NOTE:&lt;/b&gt; This will not work for content that is already encoded. For content that stored in its encoded format you will need to do some kind of replace or use XSL.&lt;/i&gt;&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How To: Solve "Ambiguous delete, unique identifier required" errors</title><link>http://sqlxml.org/faqs.aspx?faq=82</link><pubDate>Tue, 23 Apr 2002 22:46:27 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=82</guid><comments>http://sqlxml.org/faqs.aspx?faq=82#comment</comments><description>&lt;p class="faqp"&gt;
&lt;i&gt;Answer provided by Avner Aharoni&lt;/i&gt;
&lt;/p&gt;

&lt;p class="faqp"&gt;
When running an updategram to delete a record you may receive an error that says "Ambiguous delete, unique identifier required Transaction aborted." This error may be caused by your database design and might have nothing to do with ambiguous identifiers. 
&lt;/p&gt;

&lt;p class="faqp"&gt;
Make sure that your updategram is not trying to delete records that are part of a join or records that have foriegn keys. If you are still having problems run the SQL Profiler and see what is being generated by your updategram. From the query that is generated you should be able to determine why it is not able to run.
&lt;/p&gt;
</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How To: Union all with different data types using explicit</title><link>http://sqlxml.org/faqs.aspx?faq=81</link><pubDate>Tue, 23 Apr 2002 07:43:49 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=81</guid><comments>http://sqlxml.org/faqs.aspx?faq=81#comment</comments><description>&lt;p class="faqp"&gt;
&lt;i&gt;Solution provided by Denis Lienard.&lt;/i&gt;
&lt;/p&gt;

&lt;p class="faqp"&gt;
If you want to union two queries that have a column with different data types, you can use this trick to avoid casting the data as a different type. The SQL below is a simple example that demonstrates this technique.
&lt;/p&gt;

&lt;pre class="code"&gt;

CREATE TABLE test_one
(
 one_id    numeric
)
CREATE TABLE test_two1
(
 two1_id    numeric,
 one_id  numeric,
 two1_ntext ntext
)
CREATE TABLE test_two2
(
 two2_id    numeric,
 one_id  numeric,
 two2_num numeric
)

insert into test_one values(1)
insert into test_one values(2)

insert into test_two1 values(1, 1, N'some text ...' )
insert into test_two1 values(2, 2, N'some text ...' )

insert into test_two2 values(1, 1, 250 )
insert into test_two2 values(2, 2, 3000)

select
 1   as Tag,
   NULL  as Parent,
 one_id as [ONE!1!ONE_ID],
 NULL  as [TWO!2!TWO_ID],
 NULL  as [TWO!2!ONE_ID],
 NULL  as [TWO!2!TYPE],
 NULL  as [TWO!2!!cdata], /*for ntext*/
 NULL  as [TWO!2!!cdata] /*for numeric*/
from
 test_one

UNION ALL

select
  2,
  1,
      test_one.one_id,
  test_two1.two1_id,
  test_two1.one_id,
  N'NTEXT',
  test_two1.two1_ntext,
  null
from
 test_one
 INNER JOIN test_two1
  ON test_one.one_id = test_two1.one_id

UNION ALL

select
  2,
  1,
      test_one.one_id,
  test_two2.two2_id,
  test_two2.one_id,
  N'NUMERIC',
  null,
  test_two2.two2_num
from
 test_one
 INNER JOIN test_two2
  ON test_one.one_id = test_two2.one_id
ORDER BY
      [ONE!1!ONE_ID],
      [TWO!2!TWO_ID]

for xml explicit

DROP TABLE test_two2
DROP TABLE test_two1
DROP TABLE test_one

&lt;/pre&gt;

&lt;p class="faqp"&gt;
In this example it would not be possible to create this union without casting the ntext column as a varchar. By casting it as varchar you lose the large size of the ntext field which might be a problem. The solution is to specify two columns with the same name which will show up the same in the xml.
&lt;/p&gt;
</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How can I return XML without the sql namespace declaration?</title><link>http://sqlxml.org/faqs.aspx?faq=80</link><pubDate>Thu, 11 Apr 2002 20:23:06 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=80</guid><comments>http://sqlxml.org/faqs.aspx?faq=80#comment</comments><description>&lt;p class="faqp"&gt;
Instead of using something like:
&lt;/p&gt;

&lt;pre class="code"&gt;

&amp;lt;root xmlns:sql="urn:schemas-microsoft.com:xml-sql"&gt;
&amp;lt;sql:query&gt;select * from table for xml auto&amp;lt;/sql:query&gt;
&amp;lt;/root&gt;

&lt;/pre&gt;

&lt;p class="faqp"&gt;
Use something like:
&lt;/p&gt;

&lt;pre class="code"&gt;

&amp;lt;root&gt;
&amp;lt;sql:query xmlns:sql="urn:schemas-microsoft.com:xml-sql"&gt;
  select * from table for xml auto
&amp;lt;/sql:query&gt;
&amp;lt;/root&gt;

&lt;/pre&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How To: Configuring SQLXML IIS</title><link>http://sqlxml.org/faqs.aspx?faq=79</link><pubDate>Wed, 13 Feb 2002 05:13:39 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=79</guid><comments>http://sqlxml.org/faqs.aspx?faq=79#comment</comments><description>&lt;p class="faqp"&gt;
This tutorial provided by &lt;b&gt;Dan Doris&lt;/b&gt;.
&lt;/p&gt;

&lt;h3&gt;Configuring SQLXML IIS&lt;/h3&gt;

&lt;p class="faqp"&gt;
As a note, most problems I've
seen with people setting up a
virtual directory in SQLXML, tends to be from either using the
normal IIS snap-in instead of the SQLXML IIS Configuration utility or the standard
IIS setup has been changed. Prior to setting up
SQLXML you should verify your IIS installation is working
correctly apart from SQLXML (a simple test--issue a request to
&lt;a href="http://localhost"&gt;http://localhost&lt;/a&gt;). SQLXML leverages the
functionality of
IIS, if IIS is not running correctly there will be no way for
SQLXML to process your requests.
&lt;/p&gt;

&lt;p class="faqp"&gt;
Additionally this document
assumes you have read the on-line
help for setting up a SQLXML Virtual Directory, within SQL Books
On-Line or the respective web release. It is also assumed you
are comfortable working with IIS and have a working
knowledge of IIS.
&lt;/p&gt;

&lt;h3&gt;What is needed to configure a VDir for SQLXML?&lt;/h3&gt;

&lt;p class="faqp"&gt;
The minimum installation
requires both the SQL Client tools and
the version of SQLXML with the features you desire to use. IIS
does not need be on the same machine as SQL Server.
&lt;/p&gt;

&lt;h3&gt;I can't connect with the SQLXML Configuration on my local server with SQL.&lt;/h3&gt;

&lt;p class="faqp"&gt;
There are a few things you
need to ensure are installed and
running. First IIS must be set up and running on the machine.
Additionally, you must have ADMIN privileges from the currently
logged on user account. Assuming you have a standard
configuration for IIS, open an instance of Microsoft Internet
Explorer and enter the following request:
&lt;/p&gt;

&lt;h3&gt;http://localhost&lt;/h3&gt;

&lt;p class="faqp"&gt;
If you do not see two web
pages displayed, either IIS is not
running, not installed, incorrectly configured, or you have made
some change to the standard IIS configuration for the default
web site. If IIS is not running, start it, or if IIS is not installed,
install it.&lt;/p&gt;

&lt;p class="faqp"&gt;
If you didn't see the two pages display and you know for sure
IIS is installed and running, then you should open the IIS
configuration tool (for SQLXML configuration, I think this will
be about time you should use this in respect to setting up
SQLXML VDirs). I wish to &lt;b&gt;STRONGLY&lt;/b&gt;
warn about using any
tool besides the SQLXML IIS Configuration tool to update, edit, 
or view SQLXML VDirs. Before proceeding from here you will
need to know why your request to &lt;a href="http://localhost"&gt;http://localhost&lt;/a&gt;
didn't get
processed as expected. For this you should turn on IIS logging
for the default web site (or the webs site(s) you are having
trouble with). View the results of the IIS log to see more
information about your request(s).
&lt;/p&gt;

&lt;h3&gt;I have the SQLXML Client tools installed on a remote
server, and I can't connect to the remote IIS machine.&lt;/h3&gt;

&lt;p class="faqp"&gt;
There are a couple of possible
reasons for this. As the configuration
information for SQLMXL is stored in IIS's metabase, you must
have ADMIN privileges on the remote IIS machine. If you're logged
in account does have ADMIN permission on the remote machine, but
you're not able to connect, then you should go to the remote
machine and ensure everything is running locally. Verify you
are able to view the properties for the SQLXML VDirs locally and
process requests as you'd expect. Assuming everything on the
local machine works, but on the remote machine your request
still fails, or you're not able to connect from remote SQLXML IIS
Config tool, the next step is to ensure the server you're connecting to has the
correct DCOM settings. To edit the DCOM settings for SQLXML run &amp;quot;dcomcnfg.exe&amp;quot;
from the command line. In Windows XP it
will start as the Computer Manage MMC, under &amp;quot;Component
Services::Computers::MyComputer&amp;quot; you should see DCOM Settings. You'll want
to edit the values for:&lt;/p&gt;

&lt;h3&gt;Microsoft SQL Virtual Directory Control ([1|2|...])&lt;/h3&gt;

&lt;p class="faqp"&gt;
You will need to enable
user permissions so they may access and
launch the SQLVDIR.dll
&lt;/p&gt;

&lt;h3&gt;My web site works, but whenever I issue a request to one of SQLXML Virtual directories it fails.&lt;/h3&gt;

&lt;p&gt;
First, see what the actual
error is, you can do this by turning
off friendly error message from within Internet Explorer. This is found under Tools::Options::Advanced,
and is a check box. Make sure it
is unchecked.&lt;/p&gt;

&lt;p class="faqp"&gt;
A common problem here is a
SQLXML VDir was edited or modified from IIS's MMC console. I would
recommend using a new VDir for this test, one you know for certain is unique. Follow
the steps as outlined for the
&amp;quot;Nwind&amp;quot; sample in the help file. One side note, you will always
see a SQLVDIR in the normal IIS, but you should never open it to either view or
edit its properties.&lt;/p&gt;

&lt;p class="faqp"&gt;
If a permission error
still persists, then open the properties for the SQLVDIR again. On the Data
Source tab verify the drop down list for the database appear and that you see
the databases you would expect to see. If not, take your log-in
credentials and connect using the Query Analyzer. Once you are able to connect
with Query Analyzer, ensure your credentials for the SQL VDir are the same, effectively
both use the same code to connect to SQL.
&lt;/p&gt;

&lt;p class="faqp"&gt;
If the connection still
fails when you verify by connecting to the database with Query Analyzer,
I would not suggest using Integrated security, especially if your SQLXML VDir
will be accessed from an external client. You should not proceed
from here until you are able to connect using Query Analyzer. Before
proceeding it's assumed you are able to see the list of database from the data
source tab and can connect from Query Analyzer.
&lt;/p&gt;

&lt;p class="faqp"&gt;If a &amp;quot;file not
found&amp;quot; error occurs, then ensure the template or schema your request works
against is there. If your schema includes other files ensure they exists
as well. As templates and schemas are both XML, verify you are able to open
them with Internet Explorer, and if not fix any errors you find. I would
recommend taking any of the sample from SQL Books on-line, each of the samples
in SQL help work, I tried everyone one when learning SQLXML.&lt;/p&gt;

&lt;p class="faqp"&gt;
One simple test I like to
use requires turning on &amp;quot;Allow sql=...&amp;quot; queries (you should typically
never have this set for a public web site), and then issuing the following
query:
&lt;/p&gt;

&lt;p class="faqp"&gt;&lt;a href="http://localhost/VirtrualRoot?sql=select"&gt;http://localhost/VirtrualRoot?sql=select @@version&lt;/a&gt;&amp;quot;&lt;/p&gt;

&lt;p class="faqp"&gt;
You should see something
like:
&lt;/p&gt;

&lt;p class="faqp"&gt;
Microsoft &lt;b&gt;SQL Server 2000&lt;/b&gt; - 8.00.384 (Intel X86)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; May 23 2001 00:02:52 &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Copyright (c) 1988-2000 Microsoft Corporation&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Enterprise Edition on Windows NT 5.0 (Build 2195: Service Pack 2)
&lt;/p&gt;

&lt;h3&gt;*** After this don't forget to turn off URL SQL queries! ***&lt;/h3&gt;

&lt;p class="faqp"&gt;
If you didn't see the
expected results then verify what happened
to the request using the IIS logs and that you are still able to
connect with the same user credentials using Query Analyzer. 
You may want need to run SQL Profiler too to
verify if the query was received by the SQL Server as expected.
&lt;/p&gt;

&lt;p class="faqp"&gt;One last problem, which you
may rarely see. If your request fails with
file not found or file permission errors. You may need to provide access on all
the directories along the path to the final directory where your templates and
schemas files are located.
&lt;/p&gt;

&lt;p class="faqp"&gt;
For example your configuration is:
&lt;/p&gt;

&lt;table border="1" cellspacing="0" cellpadding="0"&gt;
&lt;tr&gt;
&lt;td width="180" valign="top"&gt;
&lt;p class="faqp"&gt;Virtual Name&lt;/p&gt;
&lt;/td&gt;
&lt;td width="400" valign="top"&gt;
&lt;p class="faqp"&gt;OS File Path&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
	
&lt;tr&gt;
&lt;td valign="top"&gt;
&lt;p class="faqp"&gt;Local Path for VDir&lt;/p&gt;
&lt;/td&gt;
&lt;td width="403" valign="top"&gt;
&lt;p class="faqp"&gt;c:\WebSite&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td valign="top"&gt;
&lt;p class="faqp"&gt;DBObject&lt;/p&gt;
&lt;/td&gt;
&lt;td width="403" valign="top"&gt;
&lt;p class="faqp"&gt;n/a -- no path involved.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td valign="top"&gt;
&lt;p class="faqp"&gt;Template&lt;/p&gt;
&lt;/td&gt;
&lt;td width="403" valign="top"&gt;
&lt;p class="faqp"&gt;c:\WebSite\VDirForWeb\Templates.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td valign="top"&gt;
&lt;p class="faqp"&gt;Schema&lt;/p&gt;
&lt;/td&gt;
&lt;td width="403" valign="top"&gt;
&lt;p class="faqp"&gt;c:\WebSite\VDirForWeb\Schemas&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p class="faqp"&gt;
Thus you http requests should
be something like:
&lt;/p&gt;

&lt;p class="faqp"&gt;
&lt;a href="http://%5blocalhost|www.website.com%5d/VDir/Template/Template.xml"&gt;http://[localhost|www.website.com]/VDir/Template/Template.xml&lt;/a&gt;
&lt;/p&gt;

&lt;p class="faqp"&gt;
When this condition occurs
you would need to provide read permission for the following directories:
&lt;/p&gt;

&lt;p class="faqp"&gt;&lt;b&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c:\WebSite&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c:\WebSite\VDirForWeb&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c:\WebSite\VDirForWeb\Templates
&lt;/b&gt;&lt;/p&gt;

&lt;h3&gt;I'm not able to add a Virtual Directory with 
the name I want even though I don't see a Virtual Directory with the same 
name listed in either the SQLXML Configuration tool or IIS.&lt;/h3&gt;

&lt;p class="faqp"&gt;
There are instances when
the Metabase may have an anomaly in it
where a VDir is not visible and exists in the Metabase but is
not viewable from the normal tools. To fix this problem you will
need to get a copy of MetaEdit and use it to delete the virtual
directory.
&lt;/p&gt;

&lt;p class="faqp"&gt;
You can find the MetaEdit
tool at:
&lt;/p&gt;

&lt;p class="faqp"&gt;
&lt;a href="http://support.microsoft.com/default.aspx?scid=kb;EN-US;q232068"&gt;http://support.microsoft.com/default.aspx?scid=kb;EN-US;q232068&lt;/a&gt;
&lt;/p&gt;

&lt;p class="faqp"&gt;
Most every problem I've
seen when configuring the SQLXML VDir if
these steps are followed have resolved all the problems I've
seen, or pointed to the direction needed to be taken to resolve
the problem.&lt;/p&gt;

&lt;p class="faqp"&gt;
If you try these steps and
the problem persists, provide
detailed information for your specific setup (SQL, WR version,
and any other software you think relevant), the configuration
for each machine (OS, Service pack, IP's...), how many web
sites on each IIS machine and the IP setting for each, which
will you plan on running SQLXML on. Typically less information
than this one can only guess, it is information you should have
available, if you don't know or have the information neither will anyone else
who may help you.
&lt;/p&gt;
</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How can I preserve whitespace when I use OpenXML?</title><link>http://sqlxml.org/faqs.aspx?faq=78</link><pubDate>Wed, 13 Feb 2002 04:18:43 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=78</guid><comments>http://sqlxml.org/faqs.aspx?faq=78#comment</comments><description>&lt;p class="faqp"&gt;
This is a fairly common question. One method is to wrap your text inside a CDATA tag:
&lt;/p&gt;

&lt;pre class="code"&gt;

declare @i int

exec sp_xml_preparedocument @i output, '
&amp;lt;root&gt;&amp;lt;letter_head&gt;&amp;lt;![CDATA[   123]]&gt;&amp;lt;/letter_head&gt;&amp;lt;/root&gt;
'

select * from OpenXML(@i, 'root', 2) with(letter_head varchar(20))

exec sp_xml_removedocument @i

&lt;/pre&gt;

&lt;p class="faqp"&gt;
I recently came across a better solution. You can encode your spaces using the &amp;amp;#160; character. This can be done when you create your XML or if you don't have control over the creation of the XML you can use the T-SQL replace function to replace spaces.
&lt;/p&gt;

&lt;pre class="code"&gt;

declare @i int

exec sp_xml_preparedocument @i output, '&amp;lt;root&gt;
&amp;lt;test&gt; space space  space   &amp;lt;/test&gt;
&amp;lt;test&gt;&amp;amp;#160;space&amp;amp;#160;space&amp;amp;#160;&amp;amp;#160;space&amp;amp;#160;&amp;amp;#160;&amp;amp;#160;&amp;lt;/test&gt;
&amp;lt;/root&gt;'

select '"' + test + '"' from OpenXML(@i, 'root/test') with (test varchar(50) '.')

exec sp_xml_removedocument @i

&lt;/pre&gt;

&lt;p class="faqp"&gt;
This works better than the CDATA method. If you find a better solution let me know.
&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How To: Insert identity values using XML Bulk Load.</title><link>http://sqlxml.org/faqs.aspx?faq=77</link><pubDate>Thu, 31 Jan 2002 18:55:53 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=77</guid><comments>http://sqlxml.org/faqs.aspx?faq=77#comment</comments><description>&lt;p class="faqp"&gt;
You can use XML Bulk Load to insert records into tables that use identity columns. Here is a simple example to demonstrate how this can be done. First create a table in your database.
&lt;/p&gt;

&lt;pre class="code"&gt;

CREATE TABLE Cust (
    CustomerID     int         IDENTITY PRIMARY KEY,
    CompanyName    varchar(20) NOT NULL,
    City           varchar(20) DEFAULT 'Seattle')
GO

&lt;/pre&gt;

&lt;p class="faqp"&gt;
Next create the schema for the table and save it. I have saved the schema as c:\samples\iden.xsd.
&lt;/p&gt;

&lt;pre class="code"&gt;

&amp;lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema"&gt;

  &amp;lt;xsd:element name="Customers" sql:relation="Cust" &gt;
   &amp;lt;xsd:complexType&gt;
     &amp;lt;xsd:sequence&gt;
       &amp;lt;xsd:element name="CustomerID" type="xsd:integer" /&gt;
       &amp;lt;xsd:element name="CompanyName" type="xsd:string" /&gt;
       &amp;lt;xsd:element name="City"        type="xsd:string" /&gt;
     &amp;lt;/xsd:sequence&gt;
    &amp;lt;/xsd:complexType&gt;
  &amp;lt;/xsd:element&gt;
&amp;lt;/xsd:schema&gt;

&lt;/pre&gt;

&lt;p class="faqp"&gt;
Then create some sample data to load into the database and save it.
&lt;/p&gt;

&lt;pre class="code"&gt;

&amp;lt;ROOT&gt;
  &amp;lt;Customers&gt;
    &amp;lt;CompanyName&gt;Hanari Carnes&amp;lt;/CompanyName&gt;
    &amp;lt;City&gt;NY&amp;lt;/City&gt; 
  &amp;lt;/Customers&gt;
  &amp;lt;Customers&gt;
    &amp;lt;CompanyName&gt;Toms Spezialitten&amp;lt;/CompanyName&gt;
    &amp;lt;City&gt;LA&amp;lt;/City&gt;
  &amp;lt;/Customers&gt;
  &amp;lt;Customers&gt;
    &amp;lt;CompanyName&gt;Victuailles en stock&amp;lt;/CompanyName&gt;
  &amp;lt;/Customers&gt;
&amp;lt;/ROOT&gt;


&lt;/pre&gt;

&lt;p class="faqp"&gt;
Then to load the data use the following VBScript code.
&lt;/p&gt;

&lt;pre class="code"&gt;

Set oBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkLoad.2.0")
    
oBL.ConnectionString = "provider=SQLOLEDB.1;server=(local)\VSdotNet;" &amp; _
   "database=test;Trusted_Connection=Yes;"
oBL.KeepIdentity = False

oBL.Execute "c:\samples\iden.xsd", "c:\samples\iden.xml"

&lt;/pre&gt;

&lt;p class="faqp"&gt;
Be sure to modify the connection string with your own values.
&lt;/p&gt;

&lt;p class="faqp"&gt;
The limitation on identity columns in XML Bulk Load is that you cannot use it with multiple tables. For instance, if you had an orders table that had a foreign key for the customers table, it would not be bulk loadable. This is a limitation of XML Bulk Loading.
&lt;/p&gt;
</description><dc:creator>Bryant Likes</dc:creator></item><item><title>Why can't I get valid XML from SQL Server?</title><link>http://sqlxml.org/faqs.aspx?faq=76</link><pubDate>Thu, 31 Jan 2002 03:30:00 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=76</guid><comments>http://sqlxml.org/faqs.aspx?faq=76#comment</comments><description>&lt;P class=faqp&gt;This is probably one of the most common questions asked in the newsgroups. The most common causes of this problem are: 
&lt;UL&gt;
&lt;LI&gt;Attempting to retrieve the results of an XML query with a recordset. 
&lt;LI&gt;Using Query Analyzer to get the results. 
&lt;LI&gt;Connecting to SQL Server with an ODBC connection. &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P class=faqp&gt;The correct method of retreiving XML from SQL Server is to use either streams with an SQLOLEDB connection or HTTP through the SQLXML virtual directories. To see an example of using streams take a look at two examples on this site: &lt;A HREF="/faqs.aspx?1"&gt;VB Example&lt;/A&gt; and &lt;A HREF="/faqs.aspx?2"&gt;ASP Example&lt;/A&gt;. Query Analyzer does not do very well with XML resultsets so you're better off not using it to check your XML results. ODBC also does not work because the SQLOLEDB provider is needed to stream the XML results. &lt;/P&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>How To: Create multiple element types in a schema based on a filter</title><link>http://sqlxml.org/faqs.aspx?faq=75</link><pubDate>Wed, 12 Dec 2001 20:31:49 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=75</guid><comments>http://sqlxml.org/faqs.aspx?faq=75#comment</comments><description>&lt;p class="faqp"&gt;
The question was brought up of how multiple elements can be defined off of a single table based on a filter. For example, if you have a products table that all your products are stored in, how can you define both a Book element and a Music element. To answer this question we will use the Northwind database and create a list of Employees that is based on who they report to.
&lt;/p&gt;

&lt;p class="faqp"&gt;
The two key items for this schema are the sql:limit-field and the sql:limit-value attributes. These attributes are defined in the SQLXML SDK under the XSD schemas section. The sql:limit-field is used to define which column you want to place a limit on; the sql:limit-value is used to defined what value you want to use to filter the table. 
&lt;/p&gt;

&lt;p class="faqp"&gt;
Here is the schema that I created to demonstrate this concept:
&lt;/p&gt;

&lt;pre class="code"&gt;

&amp;lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema"&gt;


 &amp;lt;xsd:element name="employees" sql:is-constant="1"&gt;
  &amp;lt;xsd:complexType&gt;
   &amp;lt;xsd:sequence&gt;
    &amp;lt;xsd:element name="salesRep" 
	sql:relation="Employees" 
	sql:limit-field="ReportsTo" 
	sql:limit-value="2"&gt;
      &amp;lt;xsd:complexType&gt;
       &amp;lt;xsd:sequence&gt;
	 &amp;lt;xsd:element name="FirstName" type="xsd:string"/&gt;
	 &amp;lt;xsd:element name="LastName" type="xsd:string"/&gt;
       &amp;lt;/xsd:sequence&gt;
       &amp;lt;xsd:attribute name="EmployeeID" type="xsd:integer" /&gt;
      &amp;lt;/xsd:complexType&gt;
    &amp;lt;/xsd:element&gt;
    &amp;lt;xsd:element name="manager" 
	sql:relation="Employees" 
	sql:limit-field="ReportsTo" 
	sql:limit-value="5"&gt;
      &amp;lt;xsd:complexType&gt;
       &amp;lt;xsd:sequence&gt;
	 &amp;lt;xsd:element name="FirstName" type="xsd:string"/&gt;
	 &amp;lt;xsd:element name="LastName" type="xsd:string"/&gt;
       &amp;lt;/xsd:sequence&gt;
       &amp;lt;xsd:attribute name="EmployeeID" type="xsd:integer" /&gt;
      &amp;lt;/xsd:complexType&gt;
    &amp;lt;/xsd:element&gt;
   &amp;lt;/xsd:sequence&gt;
  &amp;lt;/xsd:complexType&gt;
 &amp;lt;/xsd:element&gt;
&amp;lt;/xsd:schema&gt;

&lt;/pre&gt;

&lt;p class="faqp"&gt;
The result of running this schema creates the list of employees. Those that report to "2" are created with a salesRep element; those that report to "5" are created with a manager element. Based on this example you should be able to create similar schema for such things as products.
&lt;/p&gt;
</description><dc:creator>Bryant Likes</dc:creator></item><item><title>Why aren't my error messages being returned?</title><link>http://sqlxml.org/faqs.aspx?faq=74</link><pubDate>Wed, 12 Dec 2001 19:46:25 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=74</guid><comments>http://sqlxml.org/faqs.aspx?faq=74#comment</comments><description>&lt;p class="faqp"&gt;
Provided by &lt;i&gt;Martyn Johnson&lt;/i&gt;.
&lt;/p&gt;

&lt;p class="faqp"&gt;
If you're running into a problem where your error messages from SQL are not being returned, the problem may be with MDAC. This problem applies to machines running MDAC 2.6. Take a look at the following KB article for more information.
&lt;/p&gt;

&lt;p&gt;
&lt;a href="http://support.microsoft.com/directory/article.asp?ID=KB;EN-US;Q279760"&gt;FIX: MDAC 2.6 SQLOLEDB Provider Running on Windows 2000 Returns No Error Text Description &lt;/a&gt;
&lt;/p&gt;

</description><dc:creator>Bryant Likes</dc:creator></item><item><title>What is required to use SQLXML on a remote server?</title><link>http://sqlxml.org/faqs.aspx?faq=73</link><pubDate>Wed, 12 Dec 2001 19:32:41 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=73</guid><comments>http://sqlxml.org/faqs.aspx?faq=73#comment</comments><description>&lt;p class="faqp"&gt;
Provided by &lt;i&gt;Dan Doris&lt;/i&gt;.
&lt;/p&gt;

&lt;p class="faqp"&gt;
SQLXML may be installed on a web server running IIS only and may not have SQL Server 2000 installed locally.  In this configuration the IIS machine will need the SQL Server 2000 Client utilities to be installed.   The reason for the SQL Server 2000 Client utilities, they provide the communication layer needed (SQL DMO) between IIS machine and the remote SQL Server machine.    
&lt;/p&gt;

&lt;p class="faqp"&gt;
Depending on your specific setup you may have multiple IIS and SQL 2000 Servers within your organization that you will need to configure.   Given a scenario when you have more than one IIS machine, you may want to configure any of your IIS's virtual directories from one IIS machine.  Along with needing the SQL Client utilities,  one additional item you will need to make sure of is the account login used to configure the remote SQLXML virtual  directories has admin privileges on the remote IIS machine(s).  This is more of a require from IIS, as SQLXML will need to access the remote IIS's metabase, thus the need admin privileges.
&lt;/p&gt;

</description><dc:creator>Bryant Likes</dc:creator></item><item><title>Tutorial: Extending SQLXML with XSL and Objects</title><link>http://sqlxml.org/faqs.aspx?faq=72</link><pubDate>Sun, 25 Nov 2001 05:56:21 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=72</guid><comments>http://sqlxml.org/faqs.aspx?faq=72#comment</comments><description>&lt;p class="faqp"&gt;
This example is based on the Msgxml.com code. One of the features that was requested was to allow for users to have their passwords emailed to them. Instead of creating this type of function with ASP I decided to use XSL to demonstrate how this can be done.
&lt;/p&gt;

&lt;p class="faqp"&gt;
If you have already created the Msgxml.com message board you can add this code to extend your own message board. You can also use this example to extend your own SQLXML application by making use of this type of coding.
&lt;/p&gt;

&lt;p class="faqp"&gt;
First we are going to create an XML template. This template will return the user's email address and password. This template is very simple. If you're using the Msgxml.com message board you can name this file reminder.xml.
&lt;/p&gt;

&lt;pre class="code"&gt;

&amp;lt;?xml version="1.0"?&gt;
&amp;lt;root xmlns:sql="urn:schemas-microsoft-com:xml-sql" sql:xsl="reminder.xsl"&gt;

 &amp;lt;sql:header&gt;
   &amp;lt;sql:param name="userID"/&gt;
 &amp;lt;/sql:header&gt;
 
 &amp;lt;sql:query&gt;&amp;lt;![CDATA[
    
 select	userID,
		password
 from	users
 where	userID = @userID
 for xml auto
	
 ]]&gt;&amp;lt;/sql:query&gt;

&amp;lt;/root&gt;

&lt;/pre&gt;

&lt;p class="faqp"&gt;
This simply returns XML that contains the user's email and password. Next we will create the stylesheet. The XSL will contain the VBScript that is used to send an email. Normally you wouldn't think of XSL doing things like sending email, but you can extend XSL to do this type of work plus anything else that you put into your own COM DLLs. 
&lt;/p&gt;

&lt;pre class="code"&gt;

&amp;lt;?xml version='1.0'  encoding="windows-1252"?&gt;
&amp;lt;xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:vb="http://msgxml.com/vb"
&gt;
                
&amp;lt;xsl:output method="html" version="4.0"/&gt;

&amp;lt;msxsl:script language="VBScript" implements-prefix="vb"&gt;&amp;lt;![CDATA[
   
   Function SendMsg(userID, password)
   
	Dim oMg, oCf, oFds

	Set oMg = CreateObject("CDO.Message")
	Set oCf = oMg.Configuration
	Set oFds = oCf.Fields
                
	oFds("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
	oFds("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "localhost"
	oFds.Update                

	Set oFds = Nothing                

	With oMg   
		.From = "reminder@sqlxml.org"   
		.Subject = "Msgxml.com Password Reminder"   
		.TextBody = "Your msgxml.com password is: " &amp;amp; password 
		.To = userID  
		.Fields.Update   
		.send
	End With  
	
	SendMsg = "Your password has been sent to " &amp;amp; userID
	
	Set oMg = Nothing
   
   End Function
   
   
]]&amp;gt;&amp;lt;/msxsl:script&gt;


&amp;lt;xsl:template match="/"&gt;

&amp;lt;xsl:choose&gt;
&amp;lt;xsl:when test="root/users"&gt;

&amp;lt;html&gt;
&amp;lt;head&gt;
&amp;lt;title&gt;Msgxml.com Reminder&amp;lt;/title&gt;
&amp;lt;/head&gt;
&amp;lt;body&gt;

&amp;lt;p&gt;
&amp;lt;xsl:value-of select="vb:SendMsg(string(root/users/@userID), string(root/users/@password))"/&gt;
&amp;lt;/p&gt;

&amp;lt;p&gt;
&amp;lt;input type="button" onClick="javascript:window.close();" name="btnClose" value="Close"/&gt;
&amp;lt;/p&gt;

&amp;lt;/body&gt;
&amp;lt;/html&gt;
&amp;lt;/xsl:when&gt;
&amp;lt;xsl:otherwise&gt;

&amp;lt;html&gt;
&amp;lt;head&gt;
&amp;lt;title&gt;Error with Login&amp;lt;/title&gt;
&amp;lt;script language="JavaScript"&gt;
document.location.href="/getuser.asp?error=username not found";
&amp;lt;/script&gt;
&amp;lt;/head&gt;
&amp;lt;/html&gt;

&amp;lt;/xsl:otherwise&gt;
&amp;lt;/xsl:choose&gt;

&amp;lt;/xsl:template&gt;

&amp;lt;/xsl:stylesheet&gt;

&lt;/pre&gt;

&lt;p class="faqp"&gt;
So this XSL creates and sends an email message using CDO and ADO. You can also create your own COM DLLs in VB or C++ to extend your own applications. This adds a lot of functionality to SQLXML.
&lt;/p&gt;

&lt;p class="faqp"&gt;
If you're using the Msgxml.com message board, save the template as reminder.xml and the stylesheet as reminder.xsl. Put them both in the sqlxml directory and add a new template mapping to reminder.xml. Then you also have to modify the getuser.asp to add the link. 
&lt;/p&gt;

&lt;p class="faqp"&gt;
First you need to add the following javascript function to the script section:
&lt;/p&gt;

&lt;pre class="code"&gt;

function sendPass()
{

	if (frmLogin.userID.value.length == 0)
	{
		alert('Please enter your email address');
		return;
	}
	
	document.location.href = '/msg/reminder.xml?userID=' + frmLogin.userID.value;
	return;

}

&lt;/pre&gt;

&lt;p class="faqp"&gt;
Next add the follow row to the table under the password row:
&lt;/p&gt;

&lt;pre class="code"&gt;

&amp;lt;tr&gt;
&amp;lt;td&gt;&amp;lt;/td&gt;
&amp;lt;td&gt;&amp;lt;/td&gt;
&amp;lt;td class="smallb"&gt;
  &amp;amp;nbsp;&amp;amp;nbsp;
  &amp;lt;a href="#" onClick="sendPass();"&gt;Click here if you forgot your password.&amp;lt;/a&gt;
&amp;lt;/td&gt;
&amp;lt;/tr&gt;

&lt;/pre&gt;

&lt;p class="faqp"&gt;
You can test this out at &lt;a href="http://msgxml.com"&gt;Msgxml.com&lt;/a&gt;.
&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>What range of characters can  be used in XML?</title><link>http://sqlxml.org/faqs.aspx?faq=71</link><pubDate>Fri, 23 Nov 2001 02:21:46 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=71</guid><comments>http://sqlxml.org/faqs.aspx?faq=71#comment</comments><description>&lt;p class="faqp"&gt;
Many times errors occur when attempting to import or export data as XML. Invalid characters are a common nusance. For information on what range of characters if valid for XML take a look at the following page:
&lt;br&gt;
&lt;br&gt;
&lt;a href="http://www.w3.org/TR/2000/REC-xml-20001006#charsets"&gt;
http://www.w3.org/TR/2000/REC-xml-20001006#charsets
&lt;/a&gt;
&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item><item><title>Can I store my XSL in a stored procedure?</title><link>http://sqlxml.org/faqs.aspx?faq=70</link><pubDate>Fri, 23 Nov 2001 02:11:27 GMT</pubDate><guid isPermaLink="true">http://sqlxml.org/faqs.aspx?faq=70</guid><comments>http://sqlxml.org/faqs.aspx?faq=70#comment</comments><description>&lt;p class="faqp"&gt;
This is kind of an interesting question that I had some fun with. The answer is yes you can store your XSL stylesheet in a stored procedure. This may have some value in that you can manage both your XML output and your XSL stylesheets in one place.
&lt;/p&gt;

&lt;p class="faqp"&gt;
This first example is more of an interesting method than a pratical one. I am gonig to call the stored procedure that holds the XSL from an XML template. A more likely example would be to call the stored procedure from ADO. Any how this does demonstrate how you can manipulate your XSL prior to using it to transform your XML. In this example I just return the XSL as it is, but you could write a stored procedure (or ASP) to create XSL on the fly. Below is the stored procedure with the embedded XSL:
&lt;/p&gt;

&lt;pre class="code"&gt;

create proc sp_empXSL
as

select '
&amp;lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                version="1.0"&gt;
                
&amp;lt;xsl:output method="html" version="4.0" encoding="ISO-8859-1"/&gt;

&amp;lt;xsl:template match="/"&gt;

 &amp;lt;html&gt;
 &amp;lt;head&gt;
 &amp;lt;title&gt;View/Edit Employees Sample&amp;lt;/title&gt;
 &amp;lt;/head&gt;
 &amp;lt;body&gt;


 &amp;lt;ul&gt;
 
 &amp;lt;form name="frmSelect"&gt;

Choose an Employee:
 &amp;lt;select name="empID" onchange="frmSelect.submit();"&gt;
 &amp;lt;option/&gt;
&amp;lt;xsl:for-each select="root/employee"&gt;
 &amp;lt;option&gt;
  &amp;lt;xsl:attribute name="value"&gt;
    &amp;lt;xsl:value-of select="@employeeID"/&gt;
  &amp;lt;/xsl:attribute&gt;
  &amp;lt;xsl:if test="@selected"&gt;
    &amp;lt;xsl:attribute name="selected"/&gt;
  &amp;lt;/xsl:if&gt;
  &amp;lt;xsl:value-of select="@firstName"/&gt;&amp;amp;#160;&amp;lt;xsl:value-of select="@lastName"/&gt;
 &amp;lt;/option&gt;
&amp;lt;/xsl:for-each&gt;
 &amp;lt;/select&gt;

 &amp;lt;/form&gt;

&amp;lt;xsl:for-each select="root/employee[@selected=''true'']"&gt;
 &amp;lt;form name="frmEdit" method="POST"&gt;
 &amp;lt;xsl:attribute name="action"&gt;
 empx.xml?empID=&amp;lt;xsl:value-of select="@employeeID"/&gt;
 &amp;lt;/xsl:attribute&gt;

   &amp;lt;input type="hidden" name="edit" value="true"/&gt;
   &amp;lt;input type="hidden" name="empID"&gt;
    &amp;lt;xsl:attribute name="value"&gt;&amp;lt;xsl:value-of select="@employeeID"/&gt;&amp;lt;/xsl:attribute&gt;
   &amp;lt;/input&gt;

  FirstName:&amp;amp;#160;
   &amp;lt;input name="firstName" type="text"&gt;
    &amp;lt;xsl:attribute name="value"&gt;&amp;lt;xsl:value-of select="@firstName"/&gt;&amp;lt;/xsl:attribute&gt;
   &amp;lt;/input&gt;&amp;lt;br/&gt;

  LastName:&amp;amp;#160;
   &amp;lt;input name="lastName" type="text"&gt;
    &amp;lt;xsl:attribute name="value"&gt;&amp;lt;xsl:value-of select="@lastName"/&gt;&amp;lt;/xsl:attribute&gt;
   &amp;lt;/input&gt;&amp;lt;br/&gt;

  Title:&amp;amp;#160;&amp;amp;#160;&amp;amp;#160;
        &amp;amp;#160;&amp;amp;#160;&amp;amp;#160;
        &amp;amp;#160;&amp;amp;#160;&amp;amp;#160;&amp;amp;#160;
   &amp;lt;input name="title" type="text"&gt;
    &amp;lt;xsl:attribute name="value"&gt;&amp;lt;xsl:value-of select="@title"/&gt;&amp;lt;/xsl:attribute&gt;
   &amp;lt;/input&gt;&amp;lt;br/&gt;

   Birthday:&amp;amp;#160;&amp;amp;#160;&amp;amp;#160;&amp;amp;#160;
   &amp;lt;input name="birthday" type="text"&gt;
    &amp;lt;xsl:attribute name="value"&gt;&amp;lt;xsl:value-of select="@birthday"/&gt;&amp;lt;/xsl:attribute&gt;
   &amp;lt;/input&gt;&amp;lt;br/&gt;

   &amp;lt;input type="submit" name="submit" value="Save"/&gt;

 &amp;lt;/form&gt;
&amp;lt;/xsl:for-each&gt;

 &amp;lt;/ul&gt;

 &amp;lt;/body&gt;
 &amp;lt;/html&gt;
  
&amp;lt;/xsl:template&gt;  

&amp;lt;/xsl:stylesheet&gt;'

return 

go

&lt;/pre&gt;

&lt;p class="faqp"&gt;
The stylesheet is actually from a different example. It just takes some employee XML and creates an HTML form to edit/view the information. Now to get the XSL out of the stored proc we will use an XML template in this example. You could easily call the procedure using ADO streams to load it into a DOMDocument to transform some XML. Here is the template empXSL.xml:
&lt;/p&gt;

&lt;pre class="code"&gt;

&amp;lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&amp;lt;sql:query xmlns:sql="urn:schemas-microsoft-com:xml-sql"&gt;
  exec sp_empXSL
&amp;lt;/sql:query&gt;

&lt;/pre&gt;

&lt;p class="faqp"&gt;
The template is pretty simple. It just calls the stored proc and creates a valid XSL stylesheet for us to use. Now we just add this to our final template (empx.xml) which makes the actual query and generates the XML to be transformed:
&lt;/p&gt;

&lt;pre class="code"&gt;

&amp;lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&amp;lt;root xmlns:sql="urn:schemas-microsoft-com:xml-sql" sql:xsl="http://localhost/nwind/tp/empxsl.xml"&gt;

 &amp;lt;sql:header&gt;
   &amp;lt;sql:param name="empID"/&gt;
   &amp;lt;sql:param name="firstName"/&gt;
   &amp;lt;sql:param name="lastName"/&gt;
   &amp;lt;sql:param name="title"/&gt;
   &amp;lt;sql:param name="birthday"/&gt;
   &amp;lt;sql:param name="edit"/&gt;
 &amp;lt;/sql:header&gt;
 
 &amp;lt;test&gt;&amp;lt;sql:query&gt;select @lastName&amp;lt;/sql:query&gt;&amp;lt;/test&gt;

 &amp;lt;sql:query&gt;

	if (@edit='true') begin

		update employees
		set firstName = @firstName,
		lastName = @lastName,
		title = @title,
		birthdate = @birthday
		where employeeID = @empID
	end

	select	firstName,
		lastName,
		title,
		employeeID,
		convert(varchar(12), birthdate, 101) birthday,
		case when @empID = employeeID then 'true' else NULL end as selected
	from	employees employee
	for xml auto

 &amp;lt;/sql:query&gt;
 
&amp;lt;/root&gt;

&lt;/pre&gt;

&lt;p class="faqp"&gt;
It is important to notice that I reference the XSL with the full URL and not just the file name. This is required for SQL to run the empXSL template through the ISAPI dll instead of just loading it as it is.
&lt;/p&gt;

&lt;p class="faqp"&gt;
If you're not sure how to use templates take a look at the &lt;a href="http://msdn.microsoft.com/library/en-us/xmlsql/ac_xml1_114c.asp"&gt;BOL info&lt;/a&gt;.
&lt;/p&gt;
&lt;/p&gt;</description><dc:creator>Bryant Likes</dc:creator></item></channel></rss>