<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>SQL Blog of Zach Stagers</title>
	<atom:link href="http://scratchbox.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://scratchbox.co.uk</link>
	<description>www.scratchbox.co.uk</description>
	<lastBuildDate>Tue, 09 Aug 2011 22:03:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>MCITP: Database Developer 2008</title>
		<link>http://scratchbox.co.uk/2011/08/mcitp-database-developer-2008/</link>
		<comments>http://scratchbox.co.uk/2011/08/mcitp-database-developer-2008/#comments</comments>
		<pubDate>Tue, 09 Aug 2011 22:03:33 +0000</pubDate>
		<dc:creator>Zach Stagers</dc:creator>
				<category><![CDATA[Books and Learning]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[70-451]]></category>
		<category><![CDATA[Learning SQL]]></category>
		<category><![CDATA[MCITP]]></category>
		<category><![CDATA[MCTS]]></category>
		<category><![CDATA[Microsoft Certification]]></category>

		<guid isPermaLink="false">http://scratchbox.co.uk/?p=660</guid>
		<description><![CDATA[Following on from my previous post, I took the next exam (70-451) in the Database Development 2008 MCITP series on the 2nd of August and passed with 93.5%! &#160; &#160; This means that I am now a &#8220;Technology Specialist in SQL Server Developement (2008)&#8221; and a &#8220;Microsoft Certified IT Professional in Database Development (2008)&#8221;. The [...]]]></description>
			<content:encoded><![CDATA[<p>Following on from my previous post, I took the next exam (70-451) in the Database Development 2008 MCITP series on the 2nd of August and passed with 93.5%!</p>
<p>&nbsp;</p>
<p><img class="aligncenter size-full wp-image-661" title="Zach Stagers - MCITP Database Development 2008" src="http://scratchbox.co.uk/wp-content/uploads/2011/08/Zach-Stagers-MCITP-Database-Development-2008.jpg" alt="Zach Stagers - MCITP Database Development 2008" width="268" height="60" /></p>
<p>&nbsp;</p>
<p>This means that I am now a &#8220;Technology Specialist in SQL Server Developement (2008)&#8221; and a &#8220;Microsoft Certified IT Professional in Database Development (2008)&#8221;.</p>
<p>The 70-451 exam was pretty different to the 70-433. The questions were more focused on database design and data access, rather than remembering syntax.</p>
<p>The exam is also a little trickier to study for, as there aren&#8217;t any Microsoft Publications based around it. I would recommend getting hold of some practice test (I used <a href="http://www.transcender.com/" target="_blank">Transcenders</a>), studying them hard, and googling any terms you haven&#8217;t come across before or don&#8217;t feel you know well enough.</p>
<p>Next I plan on taking a break from certifications, and broadening my horizons by learning some C#! Wish me luck&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://scratchbox.co.uk/2011/08/mcitp-database-developer-2008/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>TS: Microsoft SQL Server 2008, Database Development (70-433)</title>
		<link>http://scratchbox.co.uk/2011/06/ts-microsoft-sql-server-2008-database-development-70-433/</link>
		<comments>http://scratchbox.co.uk/2011/06/ts-microsoft-sql-server-2008-database-development-70-433/#comments</comments>
		<pubDate>Thu, 30 Jun 2011 20:08:59 +0000</pubDate>
		<dc:creator>Zach Stagers</dc:creator>
				<category><![CDATA[Books and Learning]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[70-433]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Learning SQL]]></category>
		<category><![CDATA[MCTS]]></category>
		<category><![CDATA[Microsoft Certification]]></category>

		<guid isPermaLink="false">http://scratchbox.co.uk/?p=629</guid>
		<description><![CDATA[Yesterday, I took the 70-433 Microsoft exam, and I&#8217;m pleased to announce that I passed with 92% on my first attempt! This gives me the certification of Microsoft Certified Technology Specialist in SQL Server 2008 Database Development. I&#8217;ve been meaning to take one of these exams for a long time now, and I&#8217;ve finally managed [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I took the 70-433 Microsoft exam, and I&#8217;m pleased to announce that I passed with 92% on my first attempt! This gives me the certification of Microsoft Certified Technology Specialist in SQL Server 2008 Database Development.</p>
<p><img src="http://scratchbox.co.uk/wp-content/uploads/2011/06/MCTS_DBDev2008_WebEdit.png" alt="MCTS - Database Developer 2008" title="MCTS_DBDev2008_WebEdit" width="351" height="80" class="aligncenter size-full wp-image-654" /></p>
<p>I&#8217;ve been meaning to take one of these exams for a long time now, and I&#8217;ve finally managed to do it. I spent a lot of time reading and practicing, and I&#8217;m glad that it&#8217;s all payed off!</p>
<p>A big thank you to a man named <a href="http://www.certsandprogs.com/search/label/70-433#axzz1K9QOFw6v">Niall Merrigan</a>, as achieving this without his blog and 70-433 study guide would have been a lot more difficult! I recomend you check his site out if you&#8217;re going to take this exam.</p>
<p>Another resources I used was the <a href="http://www.amazon.co.uk/gp/product/0735626391/ref=as_li_ss_tl?ie=UTF8&#038;tag=httpscratchco-21&#038;linkCode=as2&#038;camp=1634&#038;creative=19450&#038;creativeASIN=0735626391">MCTS Self-Paced Training Kit (Exam 70-433): Microsoft SQL Server 2008 &#8211; Database Development Book/DVD Package</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=&#038;l=as2&#038;o=2&#038;a=0735626391" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />:</p>
<div id="AmazonLink"><iframe src="http://rcm-uk.amazon.co.uk/e/cm?lt1=_top&#038;bc1=FFFFFF&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=httpscratchco-21&#038;o=2&#038;p=8&#038;l=as4&#038;m=amazon&#038;f=ifr&#038;ref=ss_til&#038;asins=0735626391" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></div>
<p>The informartion in this book was good, and gave a very good understanding of most things covered in the exam. I found the practice tests to be fairly similar to the real deal, and gave decent examples and links to other resources (such as MSDN and Microsoft TechNet) when reviewing the answers. Overall &#8211; I would recommend using this book whilst studying for your 70-433 exam, but you will need to top your knowledge up with the links given in the answer reviews &#8211; and maybe a few visits to Niall&#8217;s blog mentioned above.</p>
<p>Many of my own blog posts are also focused on the topics covered in this exam, so be sure to check back here if you need a little help.</p>
<p>The next exam I&#8217;ll be tackling is the 70-451, this is the next exam in the series to achieve the certification of MCITP: Database Developer 2008. I&#8217;m sure that this will require much more reading. I&#8217;ll be sure to post back with more tips once I&#8217;ve finished this one too!</p>
]]></content:encoded>
			<wfw:commentRss>http://scratchbox.co.uk/2011/06/ts-microsoft-sql-server-2008-database-development-70-433/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Moving Database Objects between Schemas the quick way</title>
		<link>http://scratchbox.co.uk/2011/06/moving-database-objects-between-schemas-the-quick-way/</link>
		<comments>http://scratchbox.co.uk/2011/06/moving-database-objects-between-schemas-the-quick-way/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 03:35:36 +0000</pubDate>
		<dc:creator>Zach Stagers</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Schemas]]></category>
		<category><![CDATA[Transfer]]></category>

		<guid isPermaLink="false">http://scratchbox.co.uk/?p=624</guid>
		<description><![CDATA[I&#8217;ve been doing a bit database re-structuring lately, and I came across a nice and quick way to move database objects between schemas. Instead of dropping and recreating the object in the schema you require it to be in &#8211; simply execute the below: The above example will move the object &#8220;dbo.Sproc&#8221; into the &#8220;MySchema&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a bit database re-structuring lately, and I came across a nice and quick way to move database objects between schemas. Instead of dropping and recreating the object in the schema you require it to be in &#8211; simply execute the below:</p>
<pre class="qoate-code">
ALTER SCHEMA MySchema TRANSFER dbo.Sproc;
</pre>
<p>The above example will move the object &#8220;dbo.Sproc&#8221; into the &#8220;MySchema&#8221; schema.</p>
<p>Short and sweet.</p>
]]></content:encoded>
			<wfw:commentRss>http://scratchbox.co.uk/2011/06/moving-database-objects-between-schemas-the-quick-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Returning Tabular Data as XML with FOR XML PATH and Nested XML Queries</title>
		<link>http://scratchbox.co.uk/2011/05/returning-tabular-data-as-xml-with-for-xml-path-and-nested-xml-queries/</link>
		<comments>http://scratchbox.co.uk/2011/05/returning-tabular-data-as-xml-with-for-xml-path-and-nested-xml-queries/#comments</comments>
		<pubDate>Mon, 02 May 2011 04:52:22 +0000</pubDate>
		<dc:creator>Zach Stagers</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Elements]]></category>
		<category><![CDATA[FOR XML Path]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XSINIL]]></category>

		<guid isPermaLink="false">http://scratchbox.co.uk/?p=616</guid>
		<description><![CDATA[FOR XML PATH is the favorite amongst most Database Developers due to its ease of use, and the fact that you can build almost any XML structure with it. I&#8217;ll also be discusing nested XML Queries in this post, which kind of goes hand in hand with FOR XML PATH. More on this later. I&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p><code>FOR XML PATH</code> is the favorite amongst most Database Developers due to its ease of use, and the fact that you can build almost any XML structure with it.</p>
<p>I&#8217;ll also be discusing nested XML Queries in this post, which kind of goes hand in hand with <code>FOR XML PATH</code>. More on this later.</p>
<p>I&#8217;ll start off with an example, then explain what&#8217;s going on afterwards:</p>
<pre class="qoate-code">
SELECT P.Name AS "@Name",
       P.Age AS "@Age",
       'PersonId = ' + CAST(P.PersonId AS VARCHAR(5)) AS "comment()",
       (
        SELECT O.OrderId AS "@OrderId",
               O.Item AS "@Item"
	FROM Orders O
	WHERE O.PersonId = P.PersonId
	FOR XML PATH('Order'), TYPE
       ) 

FROM Person P

WHERE P.PersonId IN (1, 2)

FOR XML PATH('Person'), ROOT('People'), ELEMENTS XSINIL</pre>
<p>As with each of the other XML Modes (Raw, Auto, and Explicit), you&#8217;re able to change the name of your elements from the default. To achieve this use <code>FOR XML PATH('Person')</code>, this will give your rows the name &#8220;Person&#8221;. </p>
<p><code>ROOT('People')</code> specifies the root element to be wrapped around you&#8217;re XML. The keyword <code>ELEMENTS</code> in this instance doesn&#8217;t do a whole lot as the structure is placed into elements and attributes depending on the names given to the columns, but <code>XSINIL</code> cannot be specified without it. <code>XSINIL</code> is used to give you&#8217;re XML a namespace, as well as to handle NULL values.</p>
<p>The actual output of the query used aboved is as below:</p>
<pre class="qoate-code">
&lt;People xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  &lt;Person Name="Zach Stagers" Age="22"&gt;
    &lt;!--PersonId = 1--&gt;
    &lt;Order OrderId="1" Item="Bike" /&gt;
    &lt;Order OrderId="2" Item="Laptop" /&gt;
    &lt;Order OrderId="3" Item="Phone" /&gt;
    &lt;Order OrderId="4" Item="Keyboard" /&gt;
    &lt;Order OrderId="5" Item="Mouse" /&gt;
    &lt;Order OrderId="10" Item="Pizza" /&gt;
  &lt;/Person&gt;
  &lt;Person Name="Libbie Coulson" Age="24"&gt;
    &lt;!--PersonId = 2--&gt;
  &lt;/Person&gt;
&lt;/People&gt;</pre>
<p>The first two columns in the select are specified as &#8220;@Name&#8221; and &#8220;@Age&#8221;, this assigns them as attributes to the Person Element. </p>
<p>Specifying the column names as &#8220;comment()&#8221; makes the value a comment within the XML, note that this is case sensitive &#8211; &#8220;comment()&#8221; is not the same as &#8220;Comment()&#8221;.</p>
<p>One column name that I haven&#8217;t used in the example is &#8220;text()&#8221;, this sets the value as the text of the element, instead of an attribute.. for example, using this against <code>P.Name</code> turns the value into: </p>
<pre class="qoate-code"> &lt;Person Age="22"&gt;Zach Stagers&lt;/Person&gt; </pre>
<h3>Nested XML Queries</h3>
<p>Nested XML Queries are what gives the <code>FOR XML PATH</code> mode its structure. To do this, as in the example above, you would use a <a href="http://scratchbox.co.uk/2011/02/subqueries-correlated-and-noncorrelated/">correlated subquery</a>. Here, you link the subquery to the outer query on the PersonId &#8211; linking the person to their orders, and therefore listing the orders beneath the correct person.</p>
<p>The subqueries <code>FOR XML</code> statement must have <code>TYPE</code> specified. This translates the queries results into true XML, Microsoft did this for backwards compatibility reasons.</p>
]]></content:encoded>
			<wfw:commentRss>http://scratchbox.co.uk/2011/05/returning-tabular-data-as-xml-with-for-xml-path-and-nested-xml-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Returning Tabular Data as XML with FOR XML EXPLICIT</title>
		<link>http://scratchbox.co.uk/2011/04/returning-tabular-data-as-xml-with-for-xml-explicit/</link>
		<comments>http://scratchbox.co.uk/2011/04/returning-tabular-data-as-xml-with-for-xml-explicit/#comments</comments>
		<pubDate>Tue, 19 Apr 2011 17:24:58 +0000</pubDate>
		<dc:creator>Zach Stagers</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[FOR XML EXPLICIT]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://scratchbox.co.uk/?p=609</guid>
		<description><![CDATA[FOR XML EXPLICIT can be a little awkward to use to say the least, but it has it uses. The good thing about EXPLICIT, is that you can mold it into almost any structure you like. I actually found that reviewing the results as tabular data is what really helped me understand what the FOR [...]]]></description>
			<content:encoded><![CDATA[<p><code>FOR XML EXPLICIT</code> can be a little awkward to use to say the least, but it has it uses. The good thing about <code>EXPLICIT</code>, is that you can mold it into almost any structure you like.</p>
<p>I actually found that reviewing the results as tabular data is what really helped me understand what the <code>FOR XML EXPLICIT</code> query is doing.</p>
<p>Below is the example code that I&#8217;m using:</p>
<pre class="qoate-code">
SELECT
	1 AS Tag,
	NULL AS Parent,
	Name AS "Person!1!Name",
	PersonId AS "Person!1!PersonId",
	NULL AS "Orders!2!Item"
FROM Person

UNION ALL

SELECT
	2 AS Tag,
	1 AS Parent,
	NULL AS "Person!1!Name",
	PersonId AS "Person!1!PersonId",
	Item AS "Orders!2!Item"

FROM Orders

ORDER BY "Person!1!PersonId"

FOR XML EXPLICIT, ROOT('People')</pre>
<p>The first thing to note with this query, is the two columns &#8220;tag&#8221; and &#8220;parent&#8221;. You must specify these columns in order to use <code>FOR XML EXPLICIT</code>. These columns specify the hierarchy of your results. The first query will have a &#8220;tag&#8221; of 1, and a NULL &#8220;parent&#8221;, as it is the first object declared. Each subsequent &#8220;tag&#8221; and &#8220;parent&#8221; will then go up by increments of 1.</p>
<p>Another thing to note, is the column names. The value listed before the first exclamation in the column name specifies the Element Name. The value listed before the second exclamation is the &#8220;tag&#8221; that it relates to. Then the attribute name is specified. <code>ElementName!TagNumber!AttributeName</code>.</p>
<p>Lastly, it&#8217;s the <code>ORDER BY "Person!1!PersonId"</code> that really gives the final result its correct structure.</p>
<p>The XML outputted by the query specified above is as follows:</p>
<pre class="qoate-code">
&lt;People&gt;
  &lt;Person Name="Zach Stagers" PersonId="1"&gt;
    &lt;Orders Item="Bike" /&gt;
    &lt;Orders Item="Laptop" /&gt;
    &lt;Orders Item="Phone" /&gt;
    &lt;Orders Item="Keyboard" /&gt;
    &lt;Orders Item="Mouse" /&gt;
    &lt;Orders Item="Pizza" /&gt;
  &lt;/Person&gt;
  &lt;Person Name="Libbie Coulson" PersonId="2" /&gt;
&lt;/People&gt;</pre>
<p>As with <code>RAW</code> and <code>AUTO</code>, the <code>ROOT()</code> simply specifies the name of the root element. This is optional.</p>
<p>Without the aforementioned <code>ORDER BY</code> clause, all of the orders for person &#8220;Zach&#8221; would appear under &#8220;Libbie&#8221; as she is last in the list, and the <code>UNION ALL</code> will just tack the results from the second query to the end of the result. Ordering by the key between the two tables ensures that the orders against <code>PersonId</code> 1 appear under the person with <code>PersonId</code> 1.</p>
<p>As I mentioned earlier in this post, what really helped me understand what was happening with <code>FOR XML EXPLICIT</code> was seeing the results in there tabular format. To do this, simply remove the <code>FOR XML</code>line and re-run the code. In the below tabular example, I&#8217;ve removed <code>FOR XML EXPLICIT, ROOT('People')</code> from the query:</p>
<p><img src="http://scratchbox.co.uk/wp-content/uploads/2011/04/FOR-XML-EXPLICIT-Tabular.jpg" alt="FOR XML EXPLICIT - Tabular Result Example" title="FOR XML EXPLICIT - Tabular" width="399" height="197" class="aligncenter size-full wp-image-612" /></p>
]]></content:encoded>
			<wfw:commentRss>http://scratchbox.co.uk/2011/04/returning-tabular-data-as-xml-with-for-xml-explicit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using BULK INSERT</title>
		<link>http://scratchbox.co.uk/2011/04/using-bulk-insert/</link>
		<comments>http://scratchbox.co.uk/2011/04/using-bulk-insert/#comments</comments>
		<pubDate>Thu, 14 Apr 2011 23:14:37 +0000</pubDate>
		<dc:creator>Zach Stagers</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Bulk Insert]]></category>
		<category><![CDATA[Data File Type]]></category>
		<category><![CDATA[Field Terminator]]></category>
		<category><![CDATA[Insert]]></category>
		<category><![CDATA[Row Terminator]]></category>

		<guid isPermaLink="false">http://scratchbox.co.uk/?p=602</guid>
		<description><![CDATA[Using BULK INSERT is a nice and efficient way to, well, do a bulk insert! It&#8217;s can be good in situations where you have many, many records to insert &#8211; to many to fit into Excel, for example. To do a BULK INSERT, simply upload your file to the server that holds the database you&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p>Using <code>BULK INSERT</code> is a nice and efficient way to, well, do a bulk insert! It&#8217;s can be good in situations where you have many, many records to insert &#8211; to many to fit into Excel, for example.</p>
<p>To do a <code>BULK INSERT</code>, simply upload your file to the server that holds the database you&#8217;re going to be inserting into. Put the file in a relatively simple location, unless you like typing long file paths! Alternatively, you can copy and paste the file path, of course.</p>
<p>Below is the example file I&#8217;ll be using, along with the structure of the table&#8230;</p>
<p>Below is the structure of the example table I&#8217;ll be using, along with the contents of the file which will be imported.</p>
<pre class="qoate-code">
--Table

CREATE TABLE dbo.Orders
(
OrderId INT NOT NULL,
PersonId INT NOT NULL,
Item VARCHAR(50) NOT NULL
)

--Contents of Orders.txt

4, 1, Keyboard
5, 1, Mouse</pre>
<p>Once your file is in place, you&#8217;re ready to roll. </p>
<pre class="qoate-code">
BULK INSERT dbo.Orders
FROM 'C:\Orders.txt'
WITH
(
FIELDTERMINATOR = ','
ROWTERMINATOR = '\n'
)</pre>
<p>Notice the <code>WITH</code> statment after specifying the file location. I have only used a couple of the options available to the <code>BULK INSERT</code> statement. I will explain these, but a for a full list of options &#8211; see the <a href="http://msdn.microsoft.com/en-us/library/ms188365.aspx">MSDN site</a> on <code>BULK INSERT</code>.</p>
<p>The two options I&#8217;ve used in the example are the only two that you should need to insert some relatively simple data.</p>
<p><code>FIELDTERMINATOR</code> is used to specify the character used to split your data into the columns they belong to, you need to have the columns in the correct order in the file so that they are mapped correctly. Obviously, you need to pick a character that isn&#8217;t going to appear in any of the columns.</p>
<p><code>ROWTERMINATOR</code> is used to specify what denotes a new row. In my example, I&#8217;ve used <code>\n</code>, which is equal to an enviromental newline. This tells SQL that any time there is a new line in the Orders.txt file, that this should be treated as a seperate row from the line above.</p>
]]></content:encoded>
			<wfw:commentRss>http://scratchbox.co.uk/2011/04/using-bulk-insert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Returning Tabular Data as XML with FOR XML AUTO</title>
		<link>http://scratchbox.co.uk/2011/04/returning-tabular-data-as-xml-with-for-xml-auto/</link>
		<comments>http://scratchbox.co.uk/2011/04/returning-tabular-data-as-xml-with-for-xml-auto/#comments</comments>
		<pubDate>Thu, 14 Apr 2011 13:07:28 +0000</pubDate>
		<dc:creator>Zach Stagers</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Elements]]></category>
		<category><![CDATA[FOR XML AUTO]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XSINIL]]></category>

		<guid isPermaLink="false">http://scratchbox.co.uk/?p=590</guid>
		<description><![CDATA[About a week ago I posted on FOR XML RAW, this time it&#8217;s FOR XML AUTO&#8216;s turn. The two are actioned via very similar syntax, but operate slightly differently. Auto is probably named &#8220;Auto&#8221; because it performs a lot of the XML node naming for you. There are also a couple of other differences which [...]]]></description>
			<content:encoded><![CDATA[<p>About a week ago I posted on <code><a href="http://scratchbox.co.uk/2011/04/returning-tabular-data-as-xml-with-for-xml-raw/">FOR XML RAW</a></code>, this time it&#8217;s <code>FOR XML AUTO</code>&#8216;s turn.</p>
<p>The two are actioned via very similar syntax, but operate slightly differently. Auto is probably named &#8220;Auto&#8221; because it performs a lot of the XML node naming for you. There are also a couple of other differences which I&#8217;ll explain in this post.</p>
<p>With <code>FOR XML AUTO</code>, you don&#8217;t have the ability to name your row&#8217;s like you do in <code>RAW</code>. This is because their names are derived from the aliases given to the tables in your query. For example, having a table named Person, and using <code>FOR XML AUTO</code> against this table would give your rows a name of &#8220;Person&#8221;. This is the same as using <code>FOR XML RAW('Person')</code>, see below for a literal example of this:</p>
<pre class="qoate-code">
SELECT * FROM Person
FOR XML AUTO

SELECT * FROM Person
FOR XML RAW('Person')
</pre>
<p>Both of the above examples would return the below XML:</p>
<pre class="qoate-code">
&lt;Person PersonId="1" Name="Zach Stagers" Age="22" /&gt;
</pre>
<p>As with <code>RAW</code>, you can specify your root element in <code>AUTO</code>:</p>
<pre class="qoate-code">
SELECT * FROM Person
FOR XML AUTO, ROOT('People')</pre>
<p>The above would return the exact same XML data, but with the specified root element wrapped around it.</p>
<pre class="qoate-code">
&lt;People&gt;
  &lt;Person PersonId="1" Name="Zach Stagers" Age="22" /&gt;
&lt;/People&gt;</pre>
<p>The attributes of the person row can further be split out into their own elements by specifying <code>ELEMENTS</code>, and can also be given a namespace and handle SQL <code>NULL</code>&#8216;s by specifying <code>XSINIL</code></p>
<pre class="qoate-code">
SELECT * FROM Person
FOR XML AUTO, ROOT('People'), ELEMENTS, XSINIL</pre>
<p>The above example would return:</p>
<pre class="qoate-code">
&lt;People xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  &lt;Person&gt;
    &lt;PersonId&gt;1&lt;/PersonId&gt;
    &lt;Name&gt;Zach Stagers&lt;/Name&gt;
    &lt;Age&gt;22&lt;/Age&gt;
  &lt;/Person&gt;
&lt;/People&gt;</pre>
<p>There are a couple of other things to remember when using <code>FOR XML AUTO</code>, and that is that the order of your XML elements is down the order in which your columns are specified. For example using the below two queries to return tabular data would give you the same results, but using them with <code>FOR XML AUTO</code> will give you completely different XML:</p>
<pre class="qoate-code">
SELECT Person.Name,
       Orders.Item
FROM Person JOIN
     Orders ON Orders.PersonId = Person.PersonId
FOR XML AUTO, ROOT('PeopleWithOrders')</pre>
<p>Returns:</p>
<pre class="qoate-code">
&lt;PeopleWithOrders&gt;
  &lt;Person Name="Zach Stagers"&gt;
    &lt;Orders Item="Bike" /&gt;
    &lt;Orders Item="Laptop" /&gt;
    &lt;Orders Item="Phone" /&gt;
  &lt;/Person&gt;
&lt;/PeopleWithOrders&gt;</pre>
<pre class="qoate-code">
SELECT Orders.Item,
       Person.Name
FROM Person JOIN
     Orders ON Orders.PersonId = Person.PersonId
FOR XML AUTO, ROOT('PeopleWithOrders')</pre>
<p>The above query, with the columns swapped over will return:</p>
<pre class="qoate-code">
&lt;PeopleWithOrders&gt;
  &lt;Orders Item="Bike"&gt;
    &lt;Person Name="Zach Stagers" /&gt;
  &lt;/Orders&gt;
  &lt;Orders Item="Laptop"&gt;
    &lt;Person Name="Zach Stagers" /&gt;
  &lt;/Orders&gt;
  &lt;Orders Item="Phone"&gt;
    &lt;Person Name="Zach Stagers" /&gt;
  &lt;/Orders&gt;
&lt;/PeopleWithOrders&gt;</pre>
<p>So, you just need to remember that the order of the columns is what specifies the output of your XML, not the order of the tables in the <code>FROM</code> clause. The names given to the tables, however,  specifies the names given to the XML nodes.</p>
]]></content:encoded>
			<wfw:commentRss>http://scratchbox.co.uk/2011/04/returning-tabular-data-as-xml-with-for-xml-auto/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working with Optional Parameters in Stored Procedures</title>
		<link>http://scratchbox.co.uk/2011/04/working-with-optional-parameters-in-stored-procedures/</link>
		<comments>http://scratchbox.co.uk/2011/04/working-with-optional-parameters-in-stored-procedures/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 22:13:30 +0000</pubDate>
		<dc:creator>Zach Stagers</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Optional Parameters]]></category>
		<category><![CDATA[Parameters]]></category>
		<category><![CDATA[Stored Procedures]]></category>

		<guid isPermaLink="false">http://scratchbox.co.uk/?p=580</guid>
		<description><![CDATA[Using optional parameters within a stored procedure is a good way to give the caller a little more control over what is being returned. For example, if you were writing a stored procedure which was going to be used by a reporting system, and within the system, a user will have the ability to narrow [...]]]></description>
			<content:encoded><![CDATA[<p>Using optional parameters within a stored procedure is a good way to give the caller a little more control over what is being returned.</p>
<p>For example, if you were writing a stored procedure which was going to be used by a reporting system, and within the system, a user will have the ability to narrow down a search by choosing options from various drop down lists. Instead of writing many different stored procedures to handle each of the different drop down lists, you can do it all within one using optional parameters.</p>
<p>An &#8220;optional parameter&#8221; is a parameter which is set to NULL by default. The parameter must be set to NULL by default</p>
<pre class="qoate-code">
CREATE PROCEDURE dbo.GetSearchResults

@Franchise	VARCHAR(20) = NULL
@Make		VARCHAR(20) = NULL
@Colour		VARCHAR(20) = NULL

AS

SELECT Franchise,
       Make,
       Colour

FROM Vehicles

WHERE (@Franchise IS NULL OR Franchise = @Franchise) AND
      (@Make IS NULL OR Make = @Make) AND
      (@Colour IS NULL OR Colour = @Colour)</pre>
<p>In the above example, if a value isn&#8217;t passed in for one of the parameters, then the default of NULL is used. This means that the first condition in the <code>OR</code> is true because the variable is NULL, and execution continues on to the next line.</p>
<p>The above stored procedure can also be called in a few different ways:</p>
<p>1)</p>
<pre class="qoate-code">
EXEC dbo.GetSearchResults
</pre>
<p>This method simply uses all of the default values for each parameter, and the query results aren&#8217;t filtered in the where clause at all</p>
<p>2)</p>
<pre class="qoate-code">
EXEC dbo.GetSearchResults 'Volkswagen', 'Golf'
</pre>
<p>This execution method uses the values passed in and assigns them to the parameter in the same position that the parameter is declared. By that I mean, <code>@Franchise</code> is declared first in the procedure, so Volkswagen would be assigned to this variable as it&#8217;s passed in first. This method is a little dangerous, as if the value intended for <code>@Colour</code> is passed in first, no results will be returned.</p>
<p>3)</p>
<pre class="qoate-code">
EXEC dbo.GetSearchResults @Franchise = 'Volkswagen', @Colour = 'Red'
</pre>
<p>This method is probably the safest of the bunch, as it explicitly defines the value to the parameter. Any parameters that aren&#8217;t defined in the <code>EXEC</code> use the default value set in the procedrue.</p>
]]></content:encoded>
			<wfw:commentRss>http://scratchbox.co.uk/2011/04/working-with-optional-parameters-in-stored-procedures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding a list of user defined tables with schemas within a SQL Database</title>
		<link>http://scratchbox.co.uk/2011/04/finding-a-list-of-user-defined-tables-with-schemas-within-a-sql-database-2/</link>
		<comments>http://scratchbox.co.uk/2011/04/finding-a-list-of-user-defined-tables-with-schemas-within-a-sql-database-2/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 04:52:42 +0000</pubDate>
		<dc:creator>Zach Stagers</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Concatenation]]></category>
		<category><![CDATA[Schemas]]></category>
		<category><![CDATA[System Tables]]></category>
		<category><![CDATA[Tables]]></category>

		<guid isPermaLink="false">http://scratchbox.co.uk/?p=573</guid>
		<description><![CDATA[A couple of months ago I did a blog post on Finding a list of user defined tables within a SQL Database. Well, today I&#8217;ve improved upon that example to include the schema that the table belongs too as well. The reason I needed a list of tables and the schemas they belong to in [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of months ago I did a blog post on <a href="http://scratchbox.co.uk/2011/02/finding-a-list-of-user-defined-tables-within-a-sql-database/">Finding a list of user defined tables within a SQL Database</a>. Well, today I&#8217;ve improved upon that example to include the schema that the table belongs too as well.</p>
<pre class="qoate-code">
USE database
SELECT S.name + '.' + O.name [SchemaTable]

FROM sys.all_objects O JOIN
     sys.schemas S ON S.schema_id = O.schema_id

WHERE type = 'U' AND
      is_ms_shipped = 0 AND
      S.schema_id IN (5, 6, 7, 8, 9)

ORDER BY O.name</pre>
<p>The reason I needed a list of tables and the schemas they belong to in this format was because I needed to add two columns to each of them.</p>
<p>I&#8217;m far to lazy to open up each of these tables (there were 50 in total) and add the two columns manually, so I used the above SQL, as well as some concatenation to write the query for me:</p>
<pre class="qoate-code">
USE database
SELECT 'ALTER TABLE ' + S.name + '.' + O.name + ' ADD Child VARCHAR(100) NULL,
       GrandChild VARCHAR(100) NULL' [SQL]

FROM sys.all_objects O JOIN
     sys.schemas S ON S.schema_id = O.schema_id

WHERE type = 'U' AND
      is_ms_shipped = 0 AND
      S.schema_id IN (5, 6, 7, 8, 9)

ORDER BY O.name </pre>
<p>I was then able to copy the results into a new query window and execute, saving myself a lot of time.</p>
]]></content:encoded>
			<wfw:commentRss>http://scratchbox.co.uk/2011/04/finding-a-list-of-user-defined-tables-with-schemas-within-a-sql-database-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL LIKE ESCAPE Character</title>
		<link>http://scratchbox.co.uk/2011/04/sql-like-escape-character/</link>
		<comments>http://scratchbox.co.uk/2011/04/sql-like-escape-character/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 19:33:16 +0000</pubDate>
		<dc:creator>Zach Stagers</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Escape]]></category>
		<category><![CDATA[LIKE Clause]]></category>

		<guid isPermaLink="false">http://scratchbox.co.uk/?p=570</guid>
		<description><![CDATA[If you need to do a search on a field for a character that&#8217;s used by the LIKE syntax, the % for example, then you can specify an ESCAPE Character to allow you search for the symbol. To do this, simply specify your escape character, typically a value which you know won&#8217;t be returned in [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to do a search on a field for a character that&#8217;s used by the <a href="http://scratchbox.co.uk/2010/10/sql-like-clause-syntax/">LIKE syntax</a>, the % for example, then you can specify an ESCAPE Character to allow you search for the symbol.</p>
<p>To do this, simply specify your escape character, typically a value which you know won&#8217;t be returned in the result set, before the character you&#8217;re searching for.</p>
<p>For example, if a column containing someones tax band was created as a VarChar, instead of a TinyInt and users were inserting the % symbol along with the rate you might want to find and correct these records before updating the columns data type. To do something like this, you would use a query like the below:</p>
<pre class="qoate-code">
SELECT FirstName,
       Surname,
       TaxBand

WHERE TaxBand LIKE '%=%%' ESCAPE '='</pre>
<p>This query specifies = as our ESCAPE symbol, therefore the % symbol listed after it can be searched for.</p>
]]></content:encoded>
			<wfw:commentRss>http://scratchbox.co.uk/2011/04/sql-like-escape-character/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: scratchbox.co.uk @ 2012-02-23 08:09:37 -->
