<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>EXEC(@sql)</title>
	
	<link>http://www.execsql.com</link>
	<description>Blogging about SQL Server, SQL XML and related technologies.</description>
	<lastBuildDate>Fri, 08 Apr 2011 11:44:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Execsql" /><feedburner:info uri="execsql" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>SQL XML Bulk Load and Namespaces</title>
		<link>http://feedproxy.google.com/~r/Execsql/~3/XG4ji9eDzUM/sql-xml-bulk-load-and-namespaces</link>
		<comments>http://www.execsql.com/post/sql-xml-bulk-load-and-namespaces#comments</comments>
		<pubDate>Tue, 05 Apr 2011 06:10:29 +0000</pubDate>
		<dc:creator>Allan S. Hansen</dc:creator>
				<category><![CDATA[SQL XML Bulk Load]]></category>
		<category><![CDATA[bulk]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[namespace]]></category>
		<category><![CDATA[sql xml bulk load]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xsd]]></category>

		<guid isPermaLink="false">http://www.execsql.com/?p=212</guid>
		<description><![CDATA[Handling namespaces in SQL XML Bulk Load can be a headache as there’s not really much documentation about it. However while laborious, it is basically following the normal XML Schema structure. I’ll not go deep into the SQL XML Bulk Load syntax in this blog post, if you’re looking for that, I’ll advice you to <a href='http://www.execsql.com/post/sql-xml-bulk-load-and-namespaces'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Handling namespaces in SQL XML Bulk Load can be a headache as there’s not really much documentation about it. However while laborious, it is basically following the normal XML Schema structure.<br />
I’ll not go deep into the SQL XML Bulk Load syntax in this blog post, if you’re looking for that, I’ll advice you to read my <a title="SQL XML Bulk Load - basic XSD syntax" href="http://www.execsql.com/post/sql-xml-bulk-load-basic-xsd-syntax" target="_blank">SQL XML Bulk Load &#8211; basic XSD syntax </a>or <a title="SQL XML Bulk Load - more XSD syntax" href="http://www.execsql.com/post/sql-xml-bulk-load-more-xsd-syntax" target="_blank">SQL XML Bulk Load &#8211; more XSD syntax</a> blog posts before this one.</p>
<p>I’ve made this simplified example to illustrate. Suppose you have an XML file something akin to this:</p>
<pre><code style="font-size: 12px;"><span style="color: gray;">&lt;?</span><span style="color: black;">xml version</span><span style="color: blue;">=</span><span style="color: darkred;">"1.0" </span><span style="color: black;">encoding</span><span style="color: blue;">=</span><span style="color: darkred;">"utf-8"</span><span style="color: gray;">?&gt;
&lt;</span><span style="color: black;">Catalogue xmlns:sdt</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:SpecializedDatatypes-2"
           </span><span style="color: black;">xmlns</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:Catalogue-2"
           </span><span style="color: black;">xmlns:cac</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
           </span><span style="color: black;">xmlns:cbc</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
</span><span style="color: black;">           xmlns:xsi</span><span style="color: blue;">=</span><span style="color: darkred;">"http://www.w3.org/2001/XMLSchema-instance"
           </span><span style="color: black;">xsi:schemaLocation</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:Catalogue-2 UBL-Catalogue-2.0.xsd"</span><span style="color: gray;">&gt;
  &lt;</span><span style="color: black;">cbc:UBLVersionID</span><span style="color: gray;">&gt;</span><span style="color: black;">2.0</span><span style="color: gray;">&lt;/</span><span style="color: black;">cbc:UBLVersionID</span><span style="color: gray;">&gt;
   &lt;</span><span style="color: black;">cac:ValidityPeriod</span><span style="color: gray;">&gt;
    &lt;</span><span style="color: black;">cbc:StartDate</span><span style="color: gray;">&gt;</span><span style="color: black;">2011</span><span style="color: gray;">-</span><span style="color: black;">03</span><span style="color: gray;">-</span><span style="color: black;">02</span><span style="color: gray;">&lt;/</span><span style="color: black;">cbc:StartDate</span><span style="color: gray;">&gt;
  &lt;/</span><span style="color: black;">cac:ValidityPeriod</span><span style="color: gray;">&gt;
   &lt;</span><span style="color: black;">cac:CatalogueLine</span><span style="color: gray;">&gt;
    &lt;</span><span style="color: black;">cbc:ID</span><span style="color: gray;">&gt;</span><span style="color: black;">11002</span><span style="color: gray;">&lt;/</span><span style="color: black;">cbc:ID</span><span style="color: gray;">&gt;    
  &lt;/</span><span style="color: black;">cac:CatalogueLine</span><span style="color: gray;">&gt;
   &lt;</span><span style="color: black;">cac:CatalogueLine</span><span style="color: gray;">&gt;
    &lt;</span><span style="color: black;">cbc:ID</span><span style="color: gray;">&gt;</span><span style="color: black;">11003</span><span style="color: gray;">&lt;/</span><span style="color: black;">cbc:ID</span><span style="color: gray;">&gt;
  &lt;/</span><span style="color: black;">cac:CatalogueLine</span><span style="color: gray;">&gt;
&lt;/</span><span style="color: black;">Catalogue</span><span style="color: gray;">&gt;
</span></code></pre>
<p>I have two custom namespaces, and a mixture of the elements within each namespace. This is simplified, but once the logic is known, it is just expanding it with more namespaces, and more mapping.</p>
<p>First we need to make the base XML schema/XSD file. A way to handle namespaces in the XSD file is to use import statements and split each namespace element in to its own separate file. And that technique works for SQL XML Bulk Load as well.<br />
So we’ll make the base XSD like this</p>
<pre><code style="font-size: 12px;"><span style="color: gray;">&lt;?</span><span style="color: black;">xml version</span><span style="color: blue;">=</span><span style="color: darkred;">"1.0" </span><span style="color: black;">encoding</span><span style="color: blue;">=</span><span style="color: darkred;">"utf-8"</span><span style="color: gray;">?&gt;
&lt;</span><span style="color: black;">xs:schema xmlns:xsi</span><span style="color: blue;">=</span><span style="color: darkred;">"http://www.w3.org/2001/XMLSchema-instance"            
           </span><span style="color: black;">xmlns:cac</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
           </span><span style="color: black;">xmlns:cbc</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
           </span><span style="color: black;">xmlns:xs</span><span style="color: blue;">=</span><span style="color: darkred;">"http://www.w3.org/2001/XMLSchema"
           </span><span style="color: black;">xmlns:sql</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:schemas-microsoft-com:mapping-schema"</span><span style="color: gray;">&gt;

  &lt;</span><span style="color: black;">xs:annotation</span><span style="color: gray;">&gt;
    &lt;</span><span style="color: black;">xs:appinfo</span><span style="color: gray;">&gt;
      &lt;</span><span style="color: black;">sql:relationship name</span><span style="color: blue;">=</span><span style="color: darkred;">"SomeName"
                        </span><span style="color: black;">parent</span><span style="color: blue;">=</span><span style="color: darkred;">"Import"
                        </span><span style="color: black;">parent</span><span style="color: gray;">-</span><span style="color: black;">key</span><span style="color: blue;">=</span><span style="color: darkred;">"PK"
                        </span><span style="color: black;">child</span><span style="color: blue;">=</span><span style="color: darkred;">"Import2"
                        </span><span style="color: black;">child</span><span style="color: gray;">-</span><span style="color: black;">key</span><span style="color: blue;">=</span><span style="color: darkred;">"FK" </span><span style="color: gray;">/&gt;
    &lt;/</span><span style="color: black;">xs:appinfo</span><span style="color: gray;">&gt;
  &lt;/</span><span style="color: black;">xs:annotation</span><span style="color: gray;">&gt;

  &lt;</span><span style="color: black;">xs:import </span><span style="color: blue;">namespace=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" </span><span style="color: black;">schemaLocation</span><span style="color: blue;">=</span><span style="color: darkred;">"cbc.xsd" </span><span style="color: gray;">/&gt;
  &lt;</span><span style="color: black;">xs:import </span><span style="color: blue;">namespace=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" </span><span style="color: black;">schemaLocation</span><span style="color: blue;">=</span><span style="color: darkred;">"cac.xsd" </span><span style="color: gray;">/&gt;

  &lt;</span><span style="color: black;">xs:element name</span><span style="color: blue;">=</span><span style="color: darkred;">"Catalogue" </span><span style="color: black;">sql:relation</span><span style="color: blue;">=</span><span style="color: darkred;">"Import"</span><span style="color: gray;">&gt;
    &lt;</span><span style="color: black;">xs:complexType</span><span style="color: gray;">&gt;
      &lt;</span><span style="color: black;">xs:sequence</span><span style="color: gray;">&gt;
        &lt;</span><span style="color: black;">xs:element </span><span style="color: blue;">ref=</span><span style="color: darkred;">"cbc:UBLVersionID" </span><span style="color: gray;">/&gt;
        &lt;</span><span style="color: black;">xs:element </span><span style="color: blue;">ref=</span><span style="color: darkred;">"cac:ValidityPeriod" </span><span style="color: gray;">/&gt;
        &lt;</span><span style="color: black;">xs:element maxOccurs</span><span style="color: blue;">=</span><span style="color: darkred;">"unbounded" </span><span style="color: blue;">ref=</span><span style="color: darkred;">"cac:CatalogueLine" </span><span style="color: gray;">/&gt;
      &lt;/</span><span style="color: black;">xs:sequence</span><span style="color: gray;">&gt;
    &lt;/</span><span style="color: black;">xs:complexType</span><span style="color: gray;">&gt;
  &lt;/</span><span style="color: black;">xs:element</span><span style="color: gray;">&gt;
&lt;/</span><span style="color: black;">xs:schema</span><span style="color: gray;">&gt;</span></code></pre>
<p>Notice that each of the 3 elements simply has a reference to another element. So the XML tag cbc:UBLVersionID will in the XSD simply be element which ref=”cbc:UBLVersionID”, just as cac:ValidityPeriod element is also just a reference.<br />
The key here however is that I’ve imported two namespaces with a schema location</p>
<pre><code style="font-size: 12px;"><span style="color: black;"> </span><span style="color: gray;">&lt;</span><span style="color: black;">xs:import </span><span style="color: blue;">namespace=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" </span><span style="color: black;">schemaLocation</span><span style="color: blue;">=</span><span style="color: darkred;">"cbc.xsd" </span><span style="color: gray;">/&gt;
&lt;</span><span style="color: black;">xs:import </span><span style="color: blue;">namespace=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" </span><span style="color: black;">schemaLocation</span><span style="color: blue;">=</span><span style="color: darkred;">"cac.xsd" </span><span style="color: gray;">/&gt;</span></code>
</pre>
<p>These lines will tell the base schema file where to look for the specific definitions for the namespaces.</p>
<p>Now, I can make a specific schema file for the cbc namespace and the cac namespace.<br />
The cac one looks like</p>
<pre><code style="font-size: 12px;"><span style="color: gray;">&lt;?</span><span style="color: black;">xml version</span><span style="color: blue;">=</span><span style="color: darkred;">"1.0" </span><span style="color: black;">encoding</span><span style="color: blue;">=</span><span style="color: darkred;">"utf-8"</span><span style="color: gray;">?&gt;
&lt;</span><span style="color: black;">xs:schema xmlns:tns</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
                </span><span style="color: black;">attributeFormDefault</span><span style="color: blue;">=</span><span style="color: darkred;">"unqualified" </span><span style="color: black;">elementFormDefault</span><span style="color: blue;">=</span><span style="color: darkred;">"qualified"
                </span><span style="color: black;">targetNamespace</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
           </span><span style="color: black;">xmlns:sql</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:schemas-microsoft-com:mapping-schema"
           </span><span style="color: black;">xmlns:cbc</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
           </span><span style="color: black;">xmlns:xs</span><span style="color: blue;">=</span><span style="color: darkred;">"http://www.w3.org/2001/XMLSchema"</span><span style="color: gray;">&gt;
    &lt;</span><span style="color: black;">xs:import </span><span style="color: blue;">namespace=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" </span><span style="color: black;">schemaLocation</span><span style="color: blue;">=</span><span style="color: darkred;">"cbc.xsd" </span><span style="color: gray;">/&gt;

  &lt;</span><span style="color: black;">xs:element name</span><span style="color: blue;">=</span><span style="color: darkred;">"ValidityPeriod" </span><span style="color: black;">sql:is</span><span style="color: gray;">-</span><span style="color: black;">constant</span><span style="color: blue;">=</span><span style="color: darkred;">"1" </span><span style="color: gray;">&gt;
    &lt;</span><span style="color: black;">xs:complexType</span><span style="color: gray;">&gt;
      &lt;</span><span style="color: black;">xs:sequence</span><span style="color: gray;">&gt;
        &lt;</span><span style="color: black;">xs:element </span><span style="color: blue;">ref=</span><span style="color: darkred;">"cbc:StartDate" </span><span style="color: gray;">/&gt;
      &lt;/</span><span style="color: black;">xs:sequence</span><span style="color: gray;">&gt;
    &lt;/</span><span style="color: black;">xs:complexType</span><span style="color: gray;">&gt;
  &lt;/</span><span style="color: black;">xs:element</span><span style="color: gray;">&gt;
  &lt;</span><span style="color: black;">xs:element name</span><span style="color: blue;">=</span><span style="color: darkred;">"CatalogueLine" </span><span style="color: black;">sql:relationship</span><span style="color: blue;">=</span><span style="color: darkred;">"SomeName" </span><span style="color: black;">sql:relation</span><span style="color: blue;">=</span><span style="color: darkred;">"Import2"</span><span style="color: gray;">&gt;
    &lt;</span><span style="color: black;">xs:complexType</span><span style="color: gray;">&gt;
      &lt;</span><span style="color: black;">xs:sequence</span><span style="color: gray;">&gt;
        &lt;</span><span style="color: black;">xs:element </span><span style="color: blue;">ref=</span><span style="color: darkred;">"cbc:ID"  </span><span style="color: gray;">/&gt;
      &lt;/</span><span style="color: black;">xs:sequence</span><span style="color: gray;">&gt;
    &lt;/</span><span style="color: black;">xs:complexType</span><span style="color: gray;">&gt;
  &lt;/</span><span style="color: black;">xs:element</span><span style="color: gray;">&gt;
&lt;/</span><span style="color: black;">xs:schema</span><span style="color: gray;">&gt;</span></code></pre>
<p>Here the same principle of using import statement to map a namespace</p>
<pre><code style="font-size: 12px;"><span style="color: gray;">&lt;</span><span style="color: black;">xs:import </span><span style="color: blue;">namespace=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" </span><span style="color: black;">schemaLocation</span><span style="color: blue;">=</span><span style="color: darkred;">"cbc.xsd" </span><span style="color: gray;">/&gt;</span></code></pre>
<p>is used similar to the base file.</p>
<p>The cac file is used to define the contents of all cac namespace elements (the naming of the file is irrelevant, I simply named them this way to help myself keeping track). And where needed in this cac file, a reference is made to an element in the cbc file (in this case cbc:StartDate and cbc:ID).<br />
The cbc file will then qualify all cbc elements</p>
<pre><code style="font-size: 12px;"><span style="color: gray;">&lt;?</span><span style="color: black;">xml version</span><span style="color: blue;">=</span><span style="color: darkred;">"1.0" </span><span style="color: black;">encoding</span><span style="color: blue;">=</span><span style="color: darkred;">"utf-8"</span><span style="color: gray;">?&gt;
&lt;</span><span style="color: black;">xs:schema xmlns:tns</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
           </span><span style="color: black;">attributeFormDefault</span><span style="color: blue;">=</span><span style="color: darkred;">"unqualified" </span><span style="color: black;">elementFormDefault</span><span style="color: blue;">=</span><span style="color: darkred;">"qualified" </span><span style="color: black;">targetNamespace</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
           </span><span style="color: black;">xmlns:xs</span><span style="color: blue;">=</span><span style="color: darkred;">"http://www.w3.org/2001/XMLSchema"
           </span><span style="color: black;">xmlns:sql</span><span style="color: blue;">=</span><span style="color: darkred;">"urn:schemas-microsoft-com:mapping-schema"

           </span><span style="color: gray;">&gt;
  &lt;</span><span style="color: black;">xs:element name</span><span style="color: blue;">=</span><span style="color: darkred;">"UBLVersionID" </span><span style="color: black;">type</span><span style="color: blue;">=</span><span style="color: darkred;">"xs:decimal" </span><span style="color: black;">sql:field</span><span style="color: blue;">=</span><span style="color: darkred;">"VersionID" </span><span style="color: gray;">/&gt;
  &lt;</span><span style="color: black;">xs:element name</span><span style="color: blue;">=</span><span style="color: darkred;">"StartDate" </span><span style="color: black;">type</span><span style="color: blue;">=</span><span style="color: darkred;">"xs:date" </span><span style="color: black;">sql:field</span><span style="color: blue;">=</span><span style="color: darkred;">"DateX" </span><span style="color: gray;">/&gt;
  &lt;</span><span style="color: black;">xs:element name</span><span style="color: blue;">=</span><span style="color: darkred;">"ID" </span><span style="color: black;">type</span><span style="color: blue;">=</span><span style="color: darkred;">"xs:unsignedShort" </span><span style="color: black;">sql:field</span><span style="color: blue;">=</span><span style="color: darkred;">"LineID" </span><span style="color: gray;">/&gt;
&lt;/</span><span style="color: black;">xs:schema</span><span style="color: gray;">&gt;</span></code></pre>
<p>As can be seen, the sql:field, sql:relationship and sql:relation are all used across all 3 files.<br />
In the base file, I’ve defined a relationship, and I’m using the relationship in the cac file around a cbc element.</p>
<p style="text-align: left;">Running this through a SQL XML Bulk Load, and I get my data imported into two tables like this</p>
<p style="text-align: center;"><a href="http://www.execsql.com/wp-content/uploads/2011/04/xml_namespace_sql_xml_bulk_load_result.jpg"><img class="size-full wp-image-214 aligncenter" title="Result of SQL XML Bulk Load with namespaces" src="http://www.execsql.com/wp-content/uploads/2011/04/xml_namespace_sql_xml_bulk_load_result.jpg" alt="" width="209" height="125" /></a></p>
<p>This was just a simplified example, but I hope it helps to show how the syntax is build up in a XSD and then spice it up with some SQL mapping.</p>
<p>For more insight into SQL XML Bulk Load and the syntax, you can check out <a title="SQL XML Bulk Load category on EXEC(@sql)" href="http://www.execsql.com/category/sql-xml-bulk-load" target="_blank">my other posts on the subject</a>.</p>
<img src="http://feeds.feedburner.com/~r/Execsql/~4/XG4ji9eDzUM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.execsql.com/post/sql-xml-bulk-load-and-namespaces/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.execsql.com/post/sql-xml-bulk-load-and-namespaces</feedburner:origLink></item>
		<item>
		<title>COUNT on LEFT JOIN</title>
		<link>http://feedproxy.google.com/~r/Execsql/~3/FvvxiER8RDU/count-on-left-join</link>
		<comments>http://www.execsql.com/post/count-on-left-join#comments</comments>
		<pubDate>Wed, 02 Mar 2011 06:43:27 +0000</pubDate>
		<dc:creator>Allan S. Hansen</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[left join]]></category>
		<category><![CDATA[null]]></category>

		<guid isPermaLink="false">http://www.execsql.com/?p=200</guid>
		<description><![CDATA[When using COUNT with LEFT JOINs, it is worth noting that is that it does not count NULL values if counting over something different than *. This is useful if you wish to count how many of the rows fulfill some sort of join clause. Take the following example. I’ve created two tables consisting of <a href='http://www.execsql.com/post/count-on-left-join'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>When using COUNT with LEFT JOINs, it is worth noting that is that it does not count NULL values if counting over something different than *.<br />
This is useful if you wish to count how many of the rows fulfill some sort of join clause.</p>
<p>Take the following example.<br />
I’ve created two tables consisting of a unique identifier and a text column. I’ve added test data and for every two row I placed into Tabel_1, I placed one row into Table_2. Then I made a left join query which looks like:</p>
<pre><code style="font-size: 12px;"><span style="color: blue;">SELECT </span><span style="color: gray;">*
</span><span style="color: blue;">FROM </span><span style="color: black;">Table_1 </span><span style="color: blue;">AS </span><span style="color: black;">T
</span><span style="color: magenta;">LEFT </span><span style="color: blue;">JOIN </span><span style="color: black;">Table_2 </span><span style="color: blue;">AS </span><span style="color: black;">T2 </span><span style="color: blue;">ON </span><span style="color: black;">T.PK </span><span style="color: blue;">= </span><span style="color: black;">T2.FK</span></code></pre>
<p>Which provide an output like this:</p>
<div id="attachment_202" class="wp-caption alignnone" style="width: 310px"><a href="http://www.execsql.com/wp-content/uploads/2011/03/LeftJoinForCount.jpg"><img class="size-medium wp-image-202 " title="LEFT JOIN example for using COUNT | EXEC(@sql)" src="http://www.execsql.com/wp-content/uploads/2011/03/LeftJoinForCount-300x163.jpg" alt="LEFT JOIN example for using COUNT | EXEC(@sql)" width="300" height="163" /></a><p class="wp-caption-text">LEFT JOIN example for using COUNT</p></div>
<p>Now to illustrate the difference in count, run a query like this:</p>
<pre><code style="font-size: 12px;"><span style="color: blue;">SELECT </span><span style="color: magenta;">COUNT</span><span style="color: gray;">(*) </span><span style="color: blue;">AS </span><span style="color: black;">AllRows</span><span style="color: gray;">, </span><span style="color: magenta;">COUNT</span><span style="color: gray;">(</span><span style="color: black;">PK</span><span style="color: gray;">) </span><span style="color: blue;">AS </span><span style="color: black;">RowsFromLeft</span><span style="color: gray;">, </span><span style="color: magenta;">COUNT</span><span style="color: gray;">(</span><span style="color: black;">FK</span><span style="color: gray;">) </span><span style="color: blue;">AS </span><span style="color: black;">RowsFromRight
</span><span style="color: blue;">FROM </span><span style="color: black;">Table_1 </span><span style="color: blue;">AS </span><span style="color: black;">T
</span><span style="color: magenta;">LEFT </span><span style="color: blue;">JOIN </span><span style="color: black;">Table_2 </span><span style="color: blue;">AS </span><span style="color: black;">T2 </span><span style="color: blue;">ON </span><span style="color: black;">T.PK </span><span style="color: blue;">= </span><span style="color: black;">T2.FK</span></code></pre>
<p>Which provide the following output:</p>
<div id="attachment_201" class="wp-caption alignnone" style="width: 278px"><a href="http://www.execsql.com/wp-content/uploads/2011/03/CountOverLeftJoin.jpg"><img class="size-full wp-image-201 " title="COUNT result for LEFT JOIN | EXEC(@sql)" src="http://www.execsql.com/wp-content/uploads/2011/03/CountOverLeftJoin.jpg" alt="COUNT result for LEFT JOIN | EXEC(@sql)" width="268" height="42" /></a><p class="wp-caption-text">COUNT result for LEFT JOIN</p></div>
<p>I’ve seen many more or less creative solutions in my time to how to count over the Right table in a Left Join, because few people seem to read the documentation on something as simple as a COUNT.<br />
However often it is well worth it just to take a peek.</p>
<img src="http://feeds.feedburner.com/~r/Execsql/~4/FvvxiER8RDU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.execsql.com/post/count-on-left-join/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.execsql.com/post/count-on-left-join</feedburner:origLink></item>
		<item>
		<title>The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns</title>
		<link>http://feedproxy.google.com/~r/Execsql/~3/6EqkKxtJj8E/the-select-list-for-the-insert-statement-contains-fewer-items-than-the-insert-list-the-number-of-select-values-must-match-the-number-of-insert-columns</link>
		<comments>http://www.execsql.com/post/the-select-list-for-the-insert-statement-contains-fewer-items-than-the-insert-list-the-number-of-select-values-must-match-the-number-of-insert-columns#comments</comments>
		<pubDate>Wed, 23 Feb 2011 06:44:49 +0000</pubDate>
		<dc:creator>Allan S. Hansen</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[concatenate]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[insert into]]></category>
		<category><![CDATA[select]]></category>

		<guid isPermaLink="false">http://www.execsql.com/?p=189</guid>
		<description><![CDATA[I ran into the error: The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns. Well I&#8217;ve run into it often. Normally this simply means you have specified fewer items in values for an INSERT statement than you have <a href='http://www.execsql.com/post/the-select-list-for-the-insert-statement-contains-fewer-items-than-the-insert-list-the-number-of-select-values-must-match-the-number-of-insert-columns'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>I ran into the error: <em>The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns. </em><br />
Well I&#8217;ve run into it often.</p>
<p>Normally this simply means you have specified fewer items in values for an INSERT statement than you have specified you need. For example:</p>
<pre><code style="font-size: 12px;"><span style="color: blue;">INSERT INTO </span><span style="color: black;">Table1
</span><span style="color: gray;">( </span><span style="color: black;">ID</span><span style="color: gray;">, </span><span style="color: black;">[Column]</span><span style="color: gray;">, </span><span style="color: black;">Column1 </span><span style="color: gray;">)
</span><span style="color: blue;">SELECT </span><span style="color: black;">1</span><span style="color: gray;">,
</span><span style="color: red;">'Value'</span><span style="color: gray;">, </span></code></pre>
<p>Now, that&#8217;s easily rectified, by including the columns you need or removing the one from INSERT you do not need.</p>
<p>However, I also noticed that in my situation I had all the columns needed, but I was lacking a comma &#8211; like this:</p>
<pre><code style="font-size: 12px;"><span style="color: blue;">INSERT INTO </span><span style="color: black;">Table1
</span><span style="color: gray;">( </span><span style="color: black;">ID</span><span style="color: gray;">, </span><span style="color: black;">[Column]</span><span style="color: gray;">, </span><span style="color: black;">Column1 </span><span style="color: gray;">)
</span><span style="color: blue;">SELECT </span><span style="color: black;">1</span><span style="color: gray;">,
</span><span style="color: red;">'Value1'
'Value2'
</span></code></pre>
<p>Notice the lack of comma between Value1 and Value2.</p>
<p>This gives the mentioned error and not a syntax error in the SELECT statement. The reason is that SQL Server concatenates the Value1 and Value2 fields into one column, and therefore the SELECT only produces 2 columns.<br />
SQL Server will be able to concatenate two columns into one without needing to add them together, but only two columns.<br />
So if I were to forget the third comma I would get an &#8220;Incorrect syntax near Value2&#8243; error message alerting me to the issue. But forget one comma between 2 columns and you get no such information.</p>
<p>I didn&#8217;t see a mention of this in the MSDN documentation.</p>
<img src="http://feeds.feedburner.com/~r/Execsql/~4/6EqkKxtJj8E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.execsql.com/post/the-select-list-for-the-insert-statement-contains-fewer-items-than-the-insert-list-the-number-of-select-values-must-match-the-number-of-insert-columns/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.execsql.com/post/the-select-list-for-the-insert-statement-contains-fewer-items-than-the-insert-list-the-number-of-select-values-must-match-the-number-of-insert-columns</feedburner:origLink></item>
		<item>
		<title>Migrating to wordpress</title>
		<link>http://feedproxy.google.com/~r/Execsql/~3/IFIgGMPrGpw/migrating-to-wordpress</link>
		<comments>http://www.execsql.com/post/migrating-to-wordpress#comments</comments>
		<pubDate>Fri, 26 Nov 2010 08:09:02 +0000</pubDate>
		<dc:creator>Allan S. Hansen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://execsql.com/?p=5</guid>
		<description><![CDATA[I&#8217;m migrating my blog system to WordPress, so hopefully the blog will be up in full force over the weekend]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m migrating my blog system to WordPress, so hopefully the blog will be up in full force over the weekend</p>
<img src="http://feeds.feedburner.com/~r/Execsql/~4/IFIgGMPrGpw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.execsql.com/post/migrating-to-wordpress/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.execsql.com/post/migrating-to-wordpress</feedburner:origLink></item>
		<item>
		<title>@@rowcount and print</title>
		<link>http://feedproxy.google.com/~r/Execsql/~3/cxaMs_3J0MA/rowcount-and-print</link>
		<comments>http://www.execsql.com/post/rowcount-and-print#comments</comments>
		<pubDate>Mon, 07 Jun 2010 19:45:00 +0000</pubDate>
		<dc:creator>Allan S. Hansen</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[@@rowcount]]></category>

		<guid isPermaLink="false">/post/rowcount-and-print.aspx</guid>
		<description><![CDATA[Using @@rowcount with print gives unexpected result.]]></description>
			<content:encoded><![CDATA[<p>Using @@rowcount is nice at times to get the number of rows affected by the last SQL statement. However sometimes you get an unexpected result.<br />
One such situation is with the print statement which is often used as debugging statement in a query.<br />
If looking at the SQL statement:<br />
<span style="color: blue;"><span style="font-size: x-small;">SELECT </span></span><span style="color: black;"><span style="font-size: x-small;">1<br />
</span></span><span style="color: blue;"><span style="font-size: x-small;">UNION<br />
SELECT </span></span><span style="font-size: x-small;"><span style="font-size: x-small;"><span style="color: black;">2<br />
</span><span style="color: blue;">SELECT </span><span style="color: #434343;">@@ROWCOUNT</span></span> </span></p>
<p>Then the @@rowcount naturally will return the value 2.<br />
However if we insert a print statement just before the rowcount like this:</p>
<p><span style="color: blue;"><span style="font-size: x-small;">SELECT </span></span><span style="color: black;"><span style="font-size: x-small;">1<br />
</span></span><span style="color: blue;"><span style="font-size: x-small;">UNION<br />
SELECT </span></span><span style="font-size: x-small;"><span style="font-size: x-small;"><span style="color: black;">2<br />
</span><span style="color: blue;">PRINT </span></span></span><span style="font-size: x-small;"><span style="font-size: x-small;"><span style="color: red;">&#8216;test&#8217;<br />
</span><span style="color: blue;">SELECT </span><span style="color: #434343;">@@ROWCOUNT</span></span></span></p>
<p>Rowcount now suddenly is 0.</p>
<p>That means the print statement changes the rowcount despite it being just a print to the console window.<br />
Something to be aware of if relying on @@rowcount.</p>
<img src="http://feeds.feedburner.com/~r/Execsql/~4/cxaMs_3J0MA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.execsql.com/post/rowcount-and-print/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.execsql.com/post/rowcount-and-print</feedburner:origLink></item>
		<item>
		<title>The MERGE syntax – syntax and performance</title>
		<link>http://feedproxy.google.com/~r/Execsql/~3/H3LG36tCAvw/the-merge-syntax-syntax-and-performance</link>
		<comments>http://www.execsql.com/post/the-merge-syntax-syntax-and-performance#comments</comments>
		<pubDate>Fri, 21 May 2010 15:40:00 +0000</pubDate>
		<dc:creator>Allan S. Hansen</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[merge]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">/post/The-MERGE-syntax-and-performance.aspx</guid>
		<description><![CDATA[Normally when you have rows from one table you want to move over into the other, you&#8217;ll have to run both an update query to get your existing rows updated with the new values and insert query to get new rows over to your target. That means you have to write two queries. With the <a href='http://www.execsql.com/post/the-merge-syntax-syntax-and-performance'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Normally when you have rows from one table you want to move over into the other, you&#8217;ll have to run both an update query to get your existing rows updated with the new values and insert query to get new rows over to your target. That means you have to write two queries. With the merge, you only need one as it performs both the update and the insert.<br />
So let&#8217;s take a look at it.</p>
<p>First, I&#8217;ll create 2 tables with dummy data; a tblSource and a tblTarget.<br />
The scripts for creating the tables and data can be found here <a title="setup script for the merge syntax" href="/wp-content/uploads/2011/02/merge_setup_20100521.sql" target="_blank">merge_setup_20100521.sql (3.58 kb)</a>, but basically it&#8217;s just a target and source table with an ID, ValA, ValB and ValC with a clustered index on ID and some dummy data.<br />
So to do the update/insert it would look like this:<br />
<span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: blue;">UPDATE </span><span style="color: black;">MyTarget </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: blue;">SET<br />
</span><span style="color: black;">MyTarget.ValA </span><span style="color: blue;">= </span><span style="color: black;">MySource.ValA</span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: gray;">,<br />
</span><span style="color: black;">MyTarget.ValB </span><span style="color: blue;">= </span><span style="color: black;">MySource.ValB</span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: gray;">,<br />
</span><span style="color: black;">MyTarget.ValC </span><span style="color: blue;">= </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">MySource.ValC<br />
</span><span style="color: blue;">FROM </span><span style="color: black;">dbo.tblTargetInUp </span><span style="color: blue;">AS </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">MyTarget<br />
</span><span style="color: blue;">INNER JOIN </span><span style="color: black;">dbo.tblSourceInUp </span><span style="color: blue;">AS </span><span style="color: black;">MySource </span><span style="color: blue;">ON </span><span style="color: black;">MyTarget.ID </span><span style="color: blue;">= </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">MySource.ID </span></span></span></p>
<p><span style="color: blue;">INSERT INTO </span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">dbo.tblTargetInUp<br />
</span><span style="color: blue;">SELECT </span><span style="color: black;">ID</span><span style="color: gray;">, </span><span style="color: black;">ValA</span><span style="color: gray;">, </span><span style="color: black;">ValB</span><span style="color: gray;">, </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">ValC<br />
</span><span style="color: blue;">FROM </span><span style="color: black;">dbo.tblSourceInUp </span><span style="color: blue;">AS </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">MySource<br />
</span><span style="color: blue;">WHERE </span><span style="color: gray;">NOT EXISTS (</span><span style="color: blue;">SELECT </span><span style="color: black;">1 </span><span style="color: blue;">FROM </span><span style="color: black;">dbo.tblTargetInUp </span><span style="color: blue;">AS </span><span style="color: black;">MyTarget </span><span style="color: blue;">WHERE </span><span style="color: black;">MyTarget.ID </span><span style="color: blue;">= </span><span style="color: black;">MySource.ID</span></span></span><span style="color: gray;"><span style="font-size: x-small; font-family: Courier New;">)<br />
</span></span></p>
<p>If we look at the execution plan, I get an estimated cost of 9.1754 for the update and 2.48725 for the insert, meaning a combined cost of 11.66265 for 33.333 rows updated and 33.3334 rows inserted, into a target of 66.666 rows.</p>
<p>Now do remember, the estimated cost is just a number for how the query runs in my environment, it can&#8217;t be taken as a direct number and transferred to another system &#8211; I&#8217;m only interested in the relative comparison with the merge. More data, more indexes, more variations will all affect the actual numbers. Also if looking at the execution plan, it is clear it is two queries we fire, meaning that all the overhead which goes into running one query will be doubled for this. But it was how you&#8217;d have to do merges in the past.</p>
<p>Now, with the merge syntax we can do it like this:<br />
<span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">MERGE dbo.TblTargetMerge </span><span style="color: blue;">AS </span></span></span><span style="color: black;"><span style="font-size: x-small; font-family: Courier New;">MyTarget USING<br />
</span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: gray;">(<br />
</span><span style="color: blue;">SELECT </span><span style="color: gray;">* </span><span style="color: blue;">FROM </span><span style="color: black;">dbo.TblSourceMerge </span><span style="color: blue;">AS </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">TS<br />
</span><span style="color: gray;">) </span><span style="color: blue;">AS </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">MySource<br />
</span><span style="color: blue;">ON </span><span style="color: black;">MyTarget.ID </span><span style="color: blue;">= </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">MySource.ID<br />
</span><span style="color: blue;">WHEN </span><span style="color: black;">MATCHED </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: blue;">THEN<br />
UPDATE SET<br />
</span><span style="color: black;">MyTarget.ValA </span><span style="color: blue;">= </span><span style="color: black;">MySource.ValA</span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: gray;">,<br />
</span><span style="color: black;">MyTarget.ValB </span><span style="color: blue;">= </span><span style="color: black;">MySource.ValB</span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: gray;">,<br />
</span><span style="color: black;">MyTarget.ValC </span><span style="color: blue;">= </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">MySource.ValC<br />
</span><span style="color: blue;">WHEN </span><span style="color: gray;">NOT </span><span style="color: black;">MATCHED </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: blue;">THEN<br />
INSERT<br />
</span><span style="color: gray;">(</span><span style="color: black;">ID</span><span style="color: gray;">, </span><span style="color: black;">ValA</span><span style="color: gray;">, </span><span style="color: black;">ValB</span><span style="color: gray;">, </span><span style="color: black;">ValC</span></span></span><span style="color: gray;"><span style="font-size: x-small; font-family: Courier New;">)<br />
</span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: blue;">VALUES<br />
</span><span style="color: gray;">(</span><span style="color: black;">MySource.ID</span><span style="color: gray;">, </span><span style="color: black;">MySource.ValA</span><span style="color: gray;">, </span><span style="color: black;">MySource.ValB</span><span style="color: gray;">, </span><span style="color: black;">MySource.ValC</span></span></span><span style="color: gray;"><span style="font-size: x-small; font-family: Courier New;">)<br />
; </span></span></p>
<p>Note that the syntax takes both the update and insert in the WHEN MATCHED and WHEN NOT MATCHED.<br />
For the complete overview over the syntax, I&#8217;ll refer you to the documentation by microsoft: <a title="MERGE (Transact-SQL)" href="http://technet.microsoft.com/en-us/library/bb510625.aspx" target="_blank">MERGE (Transact-SQL).<br />
</a>But basically – you MERGE into a table using a source, and then define the ON clause (as you would a join), and then specify the WHEN MATCHED and the WHEN NOT MATCHED clauses.</p>
<p>One thing I&#8217;ll expand on myself though, is the OUTPUT clause which can also be coupled on to the merge. <a title="The OUTPUT clause" href="http://www.execsql.com/post/The-OUTPUT-clause" target="_blank">I mentioned the OUTPUT clause myself recently</a>.<br />
The important thing is that you can couple the $action to the output clause and get information about whether you merged the data or you inserted the data, meaning whether the row was matched or not matched.<br />
Like this:</p>
<p><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">MERGE dbo.TblTargetMerge </span><span style="color: blue;">AS </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">MyTarget USING<br />
</span><span style="color: gray;">&lt;</span><span style="color: black;">&#8230;snipped </span><span style="color: blue;">FOR </span><span style="color: black;">being brief&#8230;</span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: gray;">&gt;<br />
</span><span style="color: black;">OUTPUT $ACTION</span><span style="color: gray;">, </span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="color: black;">rest_of_select_here<br />
</span><span style="color: gray;">; </span></span></span></p>
<p>Nifty.<br />
Anyways &#8211; once we&#8217;ve build this query, we can look at the execution plan, and here we clearly see it is handled as one query.<br />
And in my case, the estimated cost is 8.407 for the same number of rows as above, meaning we’ve saved about 27.9% just by changing syntax.</p>
<p>Now, this structure I used to compare is very simple &#8211; with a simple matching of ID to ID and then just insert/overwrite everything so the actual result may vary (naturally), however the merge syntax does appear to be faster and with the added bonus of keeping the query combined into one syntax rather than divided into two different queries.</p>
<p>I must admit, I do like the merge syntax myself once I learned to read and write them.</p>
<img src="http://feeds.feedburner.com/~r/Execsql/~4/H3LG36tCAvw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.execsql.com/post/the-merge-syntax-syntax-and-performance/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.execsql.com/post/the-merge-syntax-syntax-and-performance</feedburner:origLink></item>
		<item>
		<title>A STR issue</title>
		<link>http://feedproxy.google.com/~r/Execsql/~3/jjggf7qHP-w/a-str-issue</link>
		<comments>http://www.execsql.com/post/a-str-issue#comments</comments>
		<pubDate>Thu, 20 May 2010 17:49:00 +0000</pubDate>
		<dc:creator>Allan S. Hansen</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[char]]></category>
		<category><![CDATA[str]]></category>
		<category><![CDATA[varchar]]></category>

		<guid isPermaLink="false">/post/A-STR-issue.aspx</guid>
		<description><![CDATA[I&#8217;ve seen many uses, and some misuses, of the STR function over time. One of the more &#8220;easy to spot problems&#8221; is that the STR function returns char datatype, meaning it&#8217;ll pad the result with spaces and people need to trim the result. Today I saw an even worse issue. The STR function takes a <a href='http://www.execsql.com/post/a-str-issue'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve seen many uses, and some misuses, of the STR function over time.</p>
<p>One of the more &#8220;easy to spot problems&#8221; is that the STR function returns char datatype, meaning it&#8217;ll pad the result with spaces and people need to trim the result.</p>
<p>Today I saw an even worse issue. The STR function takes a float argument. This means if you feed it a string (yes, I&#8217;ve seen it done) it will implicit convert that string to a float if it can. That in itself can cause an error, but even worse, it can cause a difficult to find bug.<br />
Suppose you have STR(&#8217;0001&#8242;) and run that, you&#8217;ll after trimming end up with the result of &#8217;1&#8242;. Why?<br />
Because &#8217;0001&#8242; is converted to a float, which &#8211; as we all know &#8211; is 1, which then will be cast to a string.</p>
<img src="http://feeds.feedburner.com/~r/Execsql/~4/jjggf7qHP-w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.execsql.com/post/a-str-issue/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.execsql.com/post/a-str-issue</feedburner:origLink></item>
		<item>
		<title>Changing domain</title>
		<link>http://feedproxy.google.com/~r/Execsql/~3/VhG2njPnSRA/changing-domain</link>
		<comments>http://www.execsql.com/post/changing-domain#comments</comments>
		<pubDate>Wed, 19 May 2010 16:11:00 +0000</pubDate>
		<dc:creator>Allan S. Hansen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">/post/Changing-domain.aspx</guid>
		<description><![CDATA[I&#8217;ve decided to change my previous domain &#8220;www.sqlstuff.dk&#8221; to this new one &#8220;www.execsql.com&#8221; &#8211; I apologize for any inconvenience which might happen as a result, but it is better done now then later All access to sqlstuff should get a 301 redirect to the new www.execsql.com domain and hopefully the transition should be smooth. So <a href='http://www.execsql.com/post/changing-domain'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve decided to change my previous domain &#8220;www.sqlstuff.dk&#8221; to this new one &#8220;www.execsql.com&#8221; &#8211; I apologize for any inconvenience which might happen as a result, but it is better done now then later <img src='http://www.execsql.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>All access to sqlstuff should get a 301 redirect to the new www.execsql.com domain and hopefully the transition should be smooth.</p>
<p>So welcome to EXEC(@sql) and bye bye to SQLStuff.dk <img src='http://www.execsql.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<img src="http://feeds.feedburner.com/~r/Execsql/~4/VhG2njPnSRA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.execsql.com/post/changing-domain/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.execsql.com/post/changing-domain</feedburner:origLink></item>
		<item>
		<title>SQL XML Bulk Load – more XSD syntax</title>
		<link>http://feedproxy.google.com/~r/Execsql/~3/rYBlSuRIVl0/sql-xml-bulk-load-more-xsd-syntax</link>
		<comments>http://www.execsql.com/post/sql-xml-bulk-load-more-xsd-syntax#comments</comments>
		<pubDate>Sat, 17 Apr 2010 13:43:00 +0000</pubDate>
		<dc:creator>Allan S. Hansen</dc:creator>
				<category><![CDATA[SQL XML Bulk Load]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[bulk]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[sql xml bulk load]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xsd]]></category>

		<guid isPermaLink="false">/post/SQL-XML-Bulk-Load-more-XSD-syntax.aspx</guid>
		<description><![CDATA[More advanced XSD syntax for SQL Server XML Bulk Load.]]></description>
			<content:encoded><![CDATA[<p>I have posted a couple of times about the <a title="SQL XML Bulk Load posts" href="http://www.execsql.com/post/category/sql-xml-bulk-load" target="_blank">XML/XSD syntax used for SQL XML Bulk Load</a> but mostly the syntax have been pretty basic, so I wanted to expand a little on the syntax with a more complex example.</p>
<p>So this time I’ll have an <a title="XML File for SQL XML Bulk Load example" href="/wp-content/uploads/2011/02/import.xml" target="_blank">XML file structured like this</a>. As can be seen this XML file consists of a couple of nested tags and is a structure that’s realistic when it comes to data imports. In fact it is a minimization of an existing one I have used for work, where I have removed duplicated structures to provide a cleaner example.<br />
The file consists of repeated details tags with content and attributes used, a product files section containing a repeated tag and a nesting of Product Lists. It should cover most of the situations which one would encounter.<br />
The only thing I do not use in this instance is composite keys, but they’re no different compared to using a singular one. I’ve written about <a title="SQL XML Bulk Load, Relationships and composite keys" href="http://www.execsql.com/post/sql-xml-bulk-load-relationships-and-composite-keys" target="_blank">composite keys and SQL XML Bulk Load here</a>.<br />
Anyways, let’s tackle the XSD for this file. (If new to the SQL XML Bulk Load, please read my <a title="SQL XML Bulk Load - basic XSD syntax" href="http://www.execsql.com/post/sql-xml-bulk-load-basic-xsd-syntax" target="_blank">SQL XML Bulk Load &#8211; basic XSD syntax</a> post first)</p>
<p>The first tag, ProductID, is simple enough and covered in the basic syntax blog post.<br />
It requires a database table containing this field. Any subsequent values placed on the same level of the XML file would be included into the same table.<br />
So we’ll make a Product table:<br />
<span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: blue;">CREATE TABLE </span><span style="color: black;">Product</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">(<br />
</span><span style="color: black;">ProductID </span><span style="color: blue;">INT </span></span></span></span><span style="color: gray;"><span style="font-family: Courier New;"><span style="font-size: small;">NOT NULL<br />
) </span></span></span></p>
<p>The XSD field will then be mapped to this table. And because I’ve named the field the same as the tag in the XML file I do not need to map the field.<br />
This gives me the segment:<br />
<span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&lt;</span><span style="color: black;">xs:element </span><span style="color: blue;">Name=</span><span style="color: darkred;">&#8220;Product&#8221; </span><span style="color: black;">sql:relation</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;Product&#8221; </span><span style="color: black;">maxOccurs</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;unbounded&#8221;</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:complexType</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:sequence</span></span></span></span><span style="font-size: small;"><span style="font-family: Courier New;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:element </span><span style="color: blue;">Name=</span><span style="color: darkred;">&#8220;ProductID&#8221; </span><span style="color: gray;">/&gt;</span></span><br />
</span></p>
<p>The details section is next. For this we need to set up a relationship because the section can be repeated several times, so we need to map it out into a related table.<br />
For this section we’ll need the table:<br />
<span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: blue;">CREATE TABLE </span><span style="color: black;">Details</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">(<br />
</span><span style="color: black;">Name </span><span style="color: blue;">VARCHAR</span><span style="color: gray;">(</span><span style="color: blue;">MAX</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">) NULL,<br />
</span><span style="color: black;">[Description] </span><span style="color: blue;">VARCHAR</span><span style="color: gray;">(</span><span style="color: blue;">MAX</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">) NULL,<br />
</span><span style="color: black;">LanguageCode </span><span style="color: blue;">VARCHAR</span><span style="color: gray;">(</span><span style="color: black;">5</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">) NOT NULL,<br />
</span><span style="color: black;">ProductID </span><span style="color: blue;">INT </span></span></span></span><span style="font-size: small;"><span style="color: gray;"><span style="font-family: Courier New;">NOT NULL<br />
)</span></span> </span></p>
<p>This can then hold the Name and Description tags from the section, and the attribute “LanguageCode” and the key ProductID to map it to the Product table/Product section.<br />
Because we need a relationship we’ll have to map it out:<br />
<span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&lt;</span><span style="color: black;">sql:relationship </span><span style="color: blue;">Name=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;Detail&#8221;<br />
</span><span style="color: black;">parent</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;Product&#8221;<br />
</span><span style="color: black;">parent</span><span style="color: gray;">-</span><span style="color: black;">key</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;ProductID&#8221;<br />
</span><span style="color: black;">child</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;Details&#8221;<br />
</span><span style="color: black;">child</span><span style="color: gray;">-</span><span style="color: black;">key</span><span style="color: blue;">=</span></span></span></span><span style="font-size: small;"><span style="font-family: Courier New;"><span style="color: darkred;">&#8220;ProductID&#8221;<br />
</span><span style="color: gray;">/&gt; </span></span> </span></p>
<p>And this then provides the following section or the XSD<br />
<span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&lt;</span><span style="color: black;">xs:element </span><span style="color: blue;">Name=</span><span style="color: darkred;">&#8220;Details&#8221; </span><span style="color: black;">sql:relation</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;Details&#8221; </span><span style="color: black;">sql:relationship</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;Detail&#8221;</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:complexType</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:sequence</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:element </span><span style="color: blue;">Name=</span><span style="color: darkred;">&#8220;Name&#8221; </span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">/&gt;<br />
&lt;</span><span style="color: black;">xs:element </span><span style="color: blue;">Name=</span><span style="color: darkred;">&#8220;Description&#8221; </span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">/&gt;<br />
&lt;/</span><span style="color: black;">xs:sequence</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:attribute </span><span style="color: blue;">Name=</span><span style="color: darkred;">&#8220;LanguageCode&#8221; </span><span style="color: blue;">Type=</span><span style="color: darkred;">&#8220;xs:string&#8221; </span><span style="color: black;">use</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;required&#8221; </span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">/&gt;<br />
&lt;/</span><span style="color: black;">xs:complexType</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;/</span><span style="color: black;">xs:element</span></span><span style="color: gray;"><span style="font-size: small;">&gt;</span> </span></span></span></p>
<p>As can be seen we map the section to the relationship and the table relation.<br />
For the section of Files, we’ll need to expand a little more. Again we need a relationship to tell the bulk load which keys to use.<br />
First the table to import into:<br />
<span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: blue;">CREATE TABLE </span><span style="color: black;">Files </span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">(<br />
</span><span style="color: black;">ProductID </span><span style="color: blue;">INT </span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">NOT NULL,<br />
</span><span style="color: black;">[Type] </span><span style="color: blue;">VARCHAR</span><span style="color: gray;">(</span><span style="color: black;">255</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">) NULL,<br />
</span><span style="color: black;">[FileName] </span><span style="color: blue;">VARCHAR</span><span style="color: gray;">(</span><span style="color: black;">255</span></span></span></span><span style="font-size: small;"><span style="color: gray;"><span style="font-family: Courier New;">) NULL<br />
)</span></span> </span><br />
Then the relationship in the XSD file<br />
<span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&lt;</span><span style="color: black;">sql:relationship </span><span style="color: blue;">Name=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;ProductFiles&#8221;<br />
</span><span style="color: black;">parent</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;Product&#8221;<br />
</span><span style="color: black;">parent</span><span style="color: gray;">-</span><span style="color: black;">key</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;ProductID&#8221;<br />
</span><span style="color: black;">child</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;Files&#8221;<br />
</span><span style="color: black;">child</span><span style="color: gray;">-</span><span style="color: black;">key</span><span style="color: blue;">=</span></span></span></span><span style="font-size: small;"><span style="font-family: Courier New;"><span style="color: darkred;">&#8220;ProductID&#8221;<br />
</span><span style="color: gray;">/&gt; </span></span> </span></p>
<p>Just like the previous one.</p>
<p>The XSD section will then look like the following:<br />
<span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&lt;</span><span style="color: black;">xs:element </span><span style="color: blue;">Name=</span><span style="color: darkred;">&#8220;ProductFiles&#8221; </span><span style="color: black;">sql:is</span><span style="color: gray;">-</span><span style="color: black;">constant</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;1&#8243;</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:complexType</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:sequence</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:element maxOccurs</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;unbounded&#8221; </span><span style="color: blue;">Name=</span><span style="color: darkred;">&#8220;FileName&#8221;<br />
</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: black;">sql:relationship</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;ProductFiles&#8221; </span><span style="color: black;">sql:relation</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;Files&#8221;</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:complexType</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:simpleContent</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:extension base</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;xs:string&#8221;</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:attribute </span><span style="color: blue;">Name=</span><span style="color: darkred;">&#8220;type&#8221; </span><span style="color: blue;">Type=</span><span style="color: darkred;">&#8220;xs:string&#8221; </span><span style="color: black;">use</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;required&#8221; </span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">/&gt;<br />
&lt;/</span><span style="color: black;">xs:extension</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;/</span><span style="color: black;">xs:simpleContent</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;/</span><span style="color: black;">xs:complexType</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;/</span><span style="color: black;">xs:element</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;/</span><span style="color: black;">xs:sequence</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;/</span><span style="color: black;">xs:complexType</span></span></span></span><span style="font-size: small;"><span style="font-family: Courier New;"><span style="color: gray;">&gt;<br />
&lt;/</span><span style="color: black;">xs:element</span><span style="color: gray;">&gt; </span></span><br />
</span><br />
We have the is-constant=”1” because this section can only exists once. The relationship and relation is put on the “FileName” element. The “tricky” part here is the xs:extension which is needed because otherwise the Bulk Load will throw an error that you’re trying to map multiple FileName elements. &lt;ERROR&gt;</p>
<p>On to the List section.<br />
Now this is a more difficult one, because here we’ll need a double relationship. We need to map the ProductID to the Type and then the Type to the multiple ProductID’s within that section. So we need the following tables:<br />
<span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: blue;">CREATE TABLE </span><span style="color: black;">ProductList </span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">(<br />
</span><span style="color: black;">[ID] </span><span style="color: blue;">INT </span><span style="color: #434343;">IDENTITY</span><span style="color: gray;">(</span><span style="color: black;">1</span><span style="color: gray;">,</span><span style="color: black;">1</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">) NOT NULL,<br />
</span><span style="color: black;">ProductID </span><span style="color: blue;">INT </span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">NOT NULL,<br />
</span><span style="color: black;">Header </span><span style="color: blue;">VARCHAR</span><span style="color: gray;">(</span><span style="color: black;">255</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">) NULL<br />
)<br />
</span><span style="color: blue;">CREATE TABLE </span><span style="color: black;">ProductListProduct</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">(<br />
</span><span style="color: black;">ProductListID </span><span style="color: blue;">INT </span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">NOT NULL,<br />
</span><span style="color: black;">ProductID </span></span></span></span><span style="font-size: small;"><span style="font-family: Courier New;"><span style="color: blue;">INT<br />
</span><span style="color: gray;">) </span></span> </span></p>
<p>Here we make an Identity ID in the ProductList which will be the foreign key in the ProductListProduct:ProductListID column.<br />
So we need the two following relationships in our XSD<br />
<span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&lt;</span><span style="color: black;">sql:relationship </span><span style="color: blue;">Name=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;ProductToList&#8221;<br />
</span><span style="color: black;">parent</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;Product&#8221;<br />
</span><span style="color: black;">parent</span><span style="color: gray;">-</span><span style="color: black;">key</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;ProductID&#8221;<br />
</span><span style="color: black;">child</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;ProductList&#8221;<br />
</span><span style="color: black;">child</span><span style="color: gray;">-</span><span style="color: black;">key</span><span style="color: blue;">=</span></span></span></span><span style="color: darkred;"><span style="font-family: Courier New;"><span style="font-size: small;">&#8220;ProductID&#8221;<br />
</span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">/&gt; </span></span></span></span></p>
<p>&lt;<span style="color: black;">sql:relationship </span><span style="color: blue;">Name=</span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;ListToProduct&#8221;<br />
</span><span style="color: black;">parent</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;ProductList&#8221;<br />
</span><span style="color: black;">parent</span><span style="color: gray;">-</span><span style="color: black;">key</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;ID&#8221;<br />
</span><span style="color: black;">child</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: darkred;">&#8220;ProductListProduct&#8221;<br />
</span><span style="color: black;">child</span><span style="color: gray;">-</span><span style="color: black;">key</span><span style="color: blue;">=</span></span></span></span><span style="color: darkred;"><span style="font-family: Courier New;"><span style="font-size: small;">&#8220;ProductListID&#8221;<br />
</span></span></span><span style="color: gray;"><span style="font-family: Courier New;"><span style="font-size: small;">/&gt; </span></span><br />
</span></p>
<p>To map the relationship. The only difference here is that the parent-key for ListToProduct is the identity column from the ProductList table, and it will be used as the child-key in the column ProductListID.<br />
And this gives the following XSD section</p>
<p><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&lt;</span><span style="color: black;">xs:element </span><span style="color: blue;">Name=</span><span style="color: darkred;">&#8220;ProductList&#8221; </span><span style="color: black;">sql:is</span><span style="color: gray;">-</span><span style="color: black;">constant</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;1&#8243;</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:complexType</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:sequence</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:element </span><span style="color: blue;">Name=</span><span style="color: darkred;">&#8220;List&#8221; </span><span style="color: black;">sql:relationship</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;ProductToList&#8221; </span><span style="color: black;">sql:relation</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;ProductList&#8221;</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:complexType</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:sequence</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:element maxOccurs</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;unbounded&#8221; </span><span style="color: blue;">Name=</span><span style="color: darkred;">&#8220;ProductID&#8221; </span><span style="color: blue;">Type=</span><span style="color: darkred;">&#8220;xs:integer&#8221;<br />
</span><span style="color: black;">sql:relationship</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;ListToProduct&#8221; </span><span style="color: black;">sql:relation</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;ProductListProduct&#8221; </span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">/&gt;<br />
&lt;/</span><span style="color: black;">xs:sequence</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;</span><span style="color: black;">xs:attribute </span><span style="color: blue;">Name=</span><span style="color: darkred;">&#8220;header&#8221; </span><span style="color: blue;">Type=</span><span style="color: darkred;">&#8220;xs:string&#8221; </span><span style="color: black;">use</span><span style="color: blue;">=</span><span style="color: darkred;">&#8220;required&#8221; </span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">/&gt;<br />
&lt;/</span><span style="color: black;">xs:complexType</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;/</span><span style="color: black;">xs:element</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;/</span><span style="color: black;">xs:sequence</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;/</span><span style="color: black;">xs:complexType</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&gt;<br />
&lt;/</span><span style="color: black;">xs:element</span></span></span></span><span style="color: gray;"><span style="font-size: x-small; font-family: Courier New;"><span style="font-size: small;">&gt; </span><br />
</span></span><br />
Again we have the is-constant annotation.<br />
We then map the element List to the relationship ProductToList as defined previously and to the table ProductList. Because header is then an attribute named the same as the field in the database, it’ll map automatically as usual.<br />
However we’ll then map the inner element “ProductID” within the List element to the next relationship ListToProduct and the table ProductListProduct.</p>
<p>The SQL XML Bulk Load will then automatically handle the identity column in the ProductList table and map it to the ProductListID column in the ProductListProduct table.</p>
<p>This gives us this following <a title="Schema file for SQL XML Bulk Load syntax" href="/wp-content/uploads/2011/02/import.xsd" target="_blank">complete XSD file</a></p>
<p>And when running the bulk load with this XML and this XSD we get the following result:</p>
<p><img src="/image.axd?picture=2010%2f4%2fsql_xml_bulk_load_result.jpg" alt="Result for importing SQL XML Bulk Load" /></p>
<img src="http://feeds.feedburner.com/~r/Execsql/~4/rYBlSuRIVl0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.execsql.com/post/sql-xml-bulk-load-more-xsd-syntax/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.execsql.com/post/sql-xml-bulk-load-more-xsd-syntax</feedburner:origLink></item>
		<item>
		<title>SQL XML Bulk Load, Relationships and composite keys</title>
		<link>http://feedproxy.google.com/~r/Execsql/~3/3W1TfDeBh_Q/sql-xml-bulk-load-relationships-and-composite-keys</link>
		<comments>http://www.execsql.com/post/sql-xml-bulk-load-relationships-and-composite-keys#comments</comments>
		<pubDate>Wed, 14 Apr 2010 10:31:00 +0000</pubDate>
		<dc:creator>Allan S. Hansen</dc:creator>
				<category><![CDATA[SQL XML Bulk Load]]></category>
		<category><![CDATA[bulk]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[sql xml bulk load]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xsd]]></category>

		<guid isPermaLink="false">/post/SQL-XML-Bulk-Load-Relationships-and-composite-keys.aspx</guid>
		<description><![CDATA[SQL XML Bulk Load, Relationships and composite keys]]></description>
			<content:encoded><![CDATA[<p>A little quick note, when needing composite keys in your relationship in SQL XML Bulk Load the syntax is as follow:</p>
<p><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: gray;">&lt;</span><span style="color: black;">sql:relationship name</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: black;">&#8220;Detail&#8221;<br />
parent</span><span style="color: blue;">=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: black;">&#8220;Import.Product&#8221;<br />
parent</span><span style="color: gray;">-</span><span style="color: blue;">KEY=</span></span></span></span><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: black;">&#8220;ProductID FileID&#8221;<br />
child</span><span style="color: blue;">=</span></span></span></span><span style="font-size: small;"><span style="font-family: Courier New;"><span style="color: black;">&#8220;Import.ProductDetails&#8221;<br />
child</span><span style="color: gray;">-</span><span style="color: blue;">KEY=</span><span style="color: black;">&#8220;ProductID FileID&#8221;</span><span style="color: gray;">/&gt;</span></span> </span></p>
<p>meaning they’re simply space separated in the relationship annotation. The composite key in this aspect are the columns ProductID and FileID.<br />
As can also be seen in this annotation, I&#8217;m using a schema qualifiaction with Import, so if you have other schemas, you can simply write them in the parent/child.</p>
<p>For more syntax, please refer to the blog post <a title="SQL XML Bulk Load - basic XSD syntax" href="http://www.execsql.com/post/sql-xml-bulk-load-basic-xsd-syntax" target="_blank">SQL XML Bulk Load &#8211; basic XSD syntax</a></p>
<img src="http://feeds.feedburner.com/~r/Execsql/~4/3W1TfDeBh_Q" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.execsql.com/post/sql-xml-bulk-load-relationships-and-composite-keys/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.execsql.com/post/sql-xml-bulk-load-relationships-and-composite-keys</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 0.746 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-01-28 13:54:23 -->

