<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>chanthou.wordpress.com</title>
	<atom:link href="http://chanpi.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://chanpi.wordpress.com</link>
	<description>What done in the past ! What doing!What gonna do. Is it for &#34;What&#34; ?</description>
	<lastBuildDate>Mon, 05 Dec 2011 10:04:00 +0000</lastBuildDate>
	<language>km</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='chanpi.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>chanthou.wordpress.com</title>
		<link>http://chanpi.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://chanpi.wordpress.com/osd.xml" title="chanthou.wordpress.com" />
	<atom:link rel='hub' href='http://chanpi.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Create New Rails App</title>
		<link>http://chanpi.wordpress.com/2011/12/05/create-new-rails-app/</link>
		<comments>http://chanpi.wordpress.com/2011/12/05/create-new-rails-app/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 09:32:22 +0000</pubDate>
		<dc:creator>chanthou.nget</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://chanpi.wordpress.com/?p=258</guid>
		<description><![CDATA[rails new api_menu -d mysql &#8211;skip-test-unit &#160; rails generate rspec:install<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=258&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>rails new api_menu -d mysql &#8211;skip-test-unit</p>
<p>&nbsp;</p>
<pre>rails generate rspec:install</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chanpi.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chanpi.wordpress.com/258/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chanpi.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chanpi.wordpress.com/258/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chanpi.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chanpi.wordpress.com/258/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chanpi.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chanpi.wordpress.com/258/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chanpi.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chanpi.wordpress.com/258/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chanpi.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chanpi.wordpress.com/258/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chanpi.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chanpi.wordpress.com/258/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=258&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chanpi.wordpress.com/2011/12/05/create-new-rails-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f8f1f15fc18223e66832ac9e47e6b3e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chanthou.nget</media:title>
		</media:content>
	</item>
		<item>
		<title>Kill Linux Process</title>
		<link>http://chanpi.wordpress.com/2011/12/01/kill-linux-process/</link>
		<comments>http://chanpi.wordpress.com/2011/12/01/kill-linux-process/#comments</comments>
		<pubDate>Thu, 01 Dec 2011 08:21:09 +0000</pubDate>
		<dc:creator>chanthou.nget</dc:creator>
				<category><![CDATA[Ubuntu Server]]></category>

		<guid isPermaLink="false">http://chanpi.wordpress.com/?p=255</guid>
		<description><![CDATA[List All Process:  $ ps List All rails Process:  $   ps auxw &#124; grep rails Kill Process:  $kill -9 1563<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=255&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>List All Process:  $ ps</p>
<p>List All rails Process:  $   ps auxw | grep rails</p>
<p>Kill Process:  $kill -9 1563</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chanpi.wordpress.com/255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chanpi.wordpress.com/255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chanpi.wordpress.com/255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chanpi.wordpress.com/255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chanpi.wordpress.com/255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chanpi.wordpress.com/255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chanpi.wordpress.com/255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chanpi.wordpress.com/255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chanpi.wordpress.com/255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chanpi.wordpress.com/255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chanpi.wordpress.com/255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chanpi.wordpress.com/255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chanpi.wordpress.com/255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chanpi.wordpress.com/255/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=255&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chanpi.wordpress.com/2011/12/01/kill-linux-process/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f8f1f15fc18223e66832ac9e47e6b3e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chanthou.nget</media:title>
		</media:content>
	</item>
		<item>
		<title>VirtualBox: No IP Address</title>
		<link>http://chanpi.wordpress.com/2011/11/25/virtualbox-no-ip-address/</link>
		<comments>http://chanpi.wordpress.com/2011/11/25/virtualbox-no-ip-address/#comments</comments>
		<pubDate>Fri, 25 Nov 2011 09:27:32 +0000</pubDate>
		<dc:creator>chanthou.nget</dc:creator>
				<category><![CDATA[Ubuntu Server]]></category>

		<guid isPermaLink="false">http://chanpi.wordpress.com/?p=253</guid>
		<description><![CDATA[Open /etc/udev/rules.d/70-persistent-net.rules, delete all of the contents, save and reboot.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=253&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Open /etc/udev/rules.d/70-persistent-net.rules, delete all of the contents, save and reboot.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chanpi.wordpress.com/253/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chanpi.wordpress.com/253/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chanpi.wordpress.com/253/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chanpi.wordpress.com/253/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chanpi.wordpress.com/253/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chanpi.wordpress.com/253/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chanpi.wordpress.com/253/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chanpi.wordpress.com/253/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chanpi.wordpress.com/253/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chanpi.wordpress.com/253/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chanpi.wordpress.com/253/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chanpi.wordpress.com/253/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chanpi.wordpress.com/253/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chanpi.wordpress.com/253/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=253&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chanpi.wordpress.com/2011/11/25/virtualbox-no-ip-address/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f8f1f15fc18223e66832ac9e47e6b3e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chanthou.nget</media:title>
		</media:content>
	</item>
		<item>
		<title>Active Record Query Interface</title>
		<link>http://chanpi.wordpress.com/2011/11/25/active-record-query-interface/</link>
		<comments>http://chanpi.wordpress.com/2011/11/25/active-record-query-interface/#comments</comments>
		<pubDate>Fri, 25 Nov 2011 01:50:43 +0000</pubDate>
		<dc:creator>chanthou.nget</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://chanpi.wordpress.com/?p=249</guid>
		<description><![CDATA[This guide covers different ways to retrieve data from the database using Active Record. By referring to this guide, you will be able to: Find records using a variety of methods and conditions Specify the order, retrieved attributes, grouping, and other properties of the found records Use eager loading to reduce the number of database [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=249&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<h2></h2>
<p>This guide covers different ways to retrieve data from the database using Active Record. By referring to this guide, you will be able to:</p>
<ul>
<li>Find records using a variety of methods and conditions</li>
<li>Specify the order, retrieved attributes, grouping, and other properties of the found records</li>
<li>Use eager loading to reduce the number of database queries needed for data retrieval</li>
<li>Use dynamic finders methods</li>
<li>Create named scopes to add custom finding behavior to your models</li>
<li>Check for the existence of particular records</li>
<li>Perform various calculations on Active Record models</li>
</ul>
<p><tt>Model.find(:all, options)</tt> is equivalent to <tt>Model.all(options)</tt></p>
<p><code># Very inefficient when users table has thousands of rows. </code></p>
<p><code>User.all.each do |user| </code></p>
<p><code> NewsLetter.weekly_deliver(user) </code></p>
<p><code>end</code></p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chanpi.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chanpi.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chanpi.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chanpi.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chanpi.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chanpi.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chanpi.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chanpi.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chanpi.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chanpi.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chanpi.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chanpi.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chanpi.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chanpi.wordpress.com/249/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=249&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chanpi.wordpress.com/2011/11/25/active-record-query-interface/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f8f1f15fc18223e66832ac9e47e6b3e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chanthou.nget</media:title>
		</media:content>
	</item>
		<item>
		<title>Mysql import and export</title>
		<link>http://chanpi.wordpress.com/2011/11/24/mysql-import-and-export/</link>
		<comments>http://chanpi.wordpress.com/2011/11/24/mysql-import-and-export/#comments</comments>
		<pubDate>Thu, 24 Nov 2011 09:53:20 +0000</pubDate>
		<dc:creator>chanthou.nget</dc:creator>
				<category><![CDATA[Mysql]]></category>

		<guid isPermaLink="false">http://chanpi.wordpress.com/?p=246</guid>
		<description><![CDATA[Export A MySQL Database &#160; &#62; mysqldump -u username -ppassword database_name &#62; FILE.sql Import A MySQL Database &#62; # mysql -u username -ppassword database_name &#60; FILE.sql<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=246&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<ul>
<li><strong></strong><span style="font-family:verdana,Arial,Helvetica;"><span style="font-family:verdana,Arial,Helvetica;"><span style="text-decoration:underline;"><strong> Export A MySQL Database</strong></span></span></span></li>
</ul>
<p>&nbsp;</p>
<p><code>&gt; <span style="font-family:verdana,Arial,Helvetica;"><span style="font-family:verdana,Arial,Helvetica;"><span style="color:green;">mysqldump -u <span style="color:red;">username</span> -p<span style="color:red;">password</span> <span style="color:red;">database_name</span> &gt; FILE.sql </span></span></span></code></p>
<ul>
<li><code><span style="font-family:verdana,Arial,Helvetica;"><span style="font-family:verdana,Arial,Helvetica;"><span style="text-decoration:underline;"><strong>Import A MySQL Database</strong></span></span></span></code></li>
</ul>
<p><span style="font-family:verdana,Arial,Helvetica;"><span style="font-family:verdana,Arial,Helvetica;">&gt;<span style="font-family:verdana,Arial,Helvetica;"><span style="font-family:verdana,Arial,Helvetica;"><span style="color:green;"> # mysql -u <span style="color:red;">username</span> -p<span style="color:red;">password</span> <span style="color:red;">database_name</span> &lt; FILE.sql </span></span></span><span style="text-decoration:underline;"><br />
</span></span></span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chanpi.wordpress.com/246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chanpi.wordpress.com/246/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chanpi.wordpress.com/246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chanpi.wordpress.com/246/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chanpi.wordpress.com/246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chanpi.wordpress.com/246/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chanpi.wordpress.com/246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chanpi.wordpress.com/246/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chanpi.wordpress.com/246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chanpi.wordpress.com/246/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chanpi.wordpress.com/246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chanpi.wordpress.com/246/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chanpi.wordpress.com/246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chanpi.wordpress.com/246/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=246&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chanpi.wordpress.com/2011/11/24/mysql-import-and-export/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f8f1f15fc18223e66832ac9e47e6b3e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chanthou.nget</media:title>
		</media:content>
	</item>
		<item>
		<title>Memoizing</title>
		<link>http://chanpi.wordpress.com/2011/10/26/feature-and-closer/</link>
		<comments>http://chanpi.wordpress.com/2011/10/26/feature-and-closer/#comments</comments>
		<pubDate>Wed, 26 Oct 2011 04:44:40 +0000</pubDate>
		<dc:creator>chanthou.nget</dc:creator>
				<category><![CDATA[Feature and Closer]]></category>

		<guid isPermaLink="false">http://chanpi.wordpress.com/?p=236</guid>
		<description><![CDATA[The Fibonacci sequence function fibonacci(x) { if (x &#60; 2) { return 1; } return fibonacci(x - 1) + fibonacci(x - 2); } Memoizing the Fibonacci sequence in a closure var fibonacci = (function () { var cache = {}; function fibonacci(x) { if (x &#60; 2) { return 1; } if (!cache[x]) { cache[x] [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=236&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre><strong><em>The Fibonacci sequence</em></strong>
function fibonacci(x) {
if (x &lt; 2) {
return 1;
}
return fibonacci(x - 1) + fibonacci(x - 2);
}
<strong><em>Memoizing the Fibonacci sequence in a closure</em></strong>
var fibonacci = (function () {
var cache = {};
function fibonacci(x) {
if (x &lt; 2) {
return 1;
}
if (!cache[x]) {
cache[x] = fibonacci(x - 1) + fibonacci(x - 2);
}
return cache[x];
}
return fibonacci;
}());
This alternative version of fibonacci runs many orders of magnitude faster
than the original one, and by extension is capable of calculating more numbers in
the sequence. However, mixing computation with caching logic is a bit ugly. Again,
we will add a function to Function.prototype to help separate concerns.
The memoize method in Listing 6.27 is capable of wrapping a method, adding
memoization without cluttering the calculation logic.
<em><strong>Listing 6.27</strong> A general purpose memoize method</em>
if (!Function.prototype.memoize) {
Function.prototype.memoize = function () {
var cache = {};
var func = this;
return function (x) {
if (!(x in cache)) {
cache[x] = func.call(this, x);
}
return cache[x];
};
};
}
This method offers a clean way to memoize functions, as seen in Listing 6.28.
<strong>Listing 6.28</strong> Memoizing the fibonacci function
TestCase("FibonacciTest", {
"test calculate high fib value with memoization":
function () {
var fibonacciFast = fibonacci.memoize();
assertEquals(1346269, fibonacciFast(30));
}
});</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chanpi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chanpi.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chanpi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chanpi.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chanpi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chanpi.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chanpi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chanpi.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chanpi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chanpi.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chanpi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chanpi.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chanpi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chanpi.wordpress.com/236/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=236&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chanpi.wordpress.com/2011/10/26/feature-and-closer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f8f1f15fc18223e66832ac9e47e6b3e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chanthou.nget</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL Cursor in Stored Procedures</title>
		<link>http://chanpi.wordpress.com/2011/10/18/sql-cursor-in-stored-procedures/</link>
		<comments>http://chanpi.wordpress.com/2011/10/18/sql-cursor-in-stored-procedures/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 06:35:23 +0000</pubDate>
		<dc:creator>chanthou.nget</dc:creator>
				<category><![CDATA[SQL Stored Procedures]]></category>

		<guid isPermaLink="false">http://chanpi.wordpress.com/?p=233</guid>
		<description><![CDATA[Summary:In this tutorial, you will learn how to use database cursor in stored procedure to loop over a result set in MySQL. MySQL supports cursor in stored procedures, functions and triggers. Cursor is used to iterate through a set of rows, which returned by a query, and process individual row. Currently with all versions greater 5.x, MySQL [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=233&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<p><strong>Summary</strong><em>:In this tutorial, you will learn how to use database cursor in stored procedure to loop over a result set in MySQL.</em></p>
<p>MySQL supports cursor in stored procedures, functions and triggers. Cursor is used to iterate through a set of rows, which returned by a query, and process individual row. Currently with all versions greater 5.x, MySQL cursor has following properties:</p>
<ul type="disc">
<li>Read only: it means you cannot update the cursor.</li>
<li>Non-scrollable: it only can traverse in one direction and cannot skip, move back or forth in result set.</li>
<li>Asensitive: you should avoid update table while open a cursor on that table otherwise you may get unexpected results.</li>
</ul>
<p>MySQL supports following statements for working with cursor.</p>
<p>First you have to declare a cursor using DECLARE statement:</p>
</div>
<div id="highlighter_227433">
<div>
<div>
<pre>DELIMITER $$
 DROP PROCEDURE IF EXISTS CursorProc$$
 CREATE PROCEDURE CursorProc()
 BEGIN
 DECLARE  no_more_products, quantity_in_stock INT DEFAULT 0;
 DECLARE  prd_code VARCHAR(255);
             DECLARE  cur_product CURSOR FOR
 SELECT  productCode FROM products;
   DECLARE  CONTINUE HANDLER FOR NOT FOUND
 SET  no_more_products = 1;

 /* for  loggging information */
 CREATE  TABLE infologs (
 Id int(11) NOT NULL AUTO_INCREMENT,
 Msg varchar(255) NOT NULL,
 PRIMARY KEY (Id)
 );
 OPEN  cur_product;

 FETCH  cur_product INTO prd_code;
 REPEAT
 SELECT  quantityInStock INTO quantity_in_stock
 FROM  products
 WHERE  productCode = prd_code;

 IF  quantity_in_stock &lt; 100 THEN
 INSERT  INTO infologs(msg)
 VALUES  (prd_code);
 END  IF;
 FETCH  cur_product INTO prd_code;
 UNTIL  no_more_products = 1
 END REPEAT;
 CLOSE  cur_product;
 SELECT *  FROM infologs;
 DROP TABLE  infologs;
 END$$
 DELIMITER;</pre>
<p><span style="font-size:small;"><span class="Apple-style-span" style="line-height:normal;"><br />
</span></span></div>
</div>
</div>
<div id="highlighter_670995"></div>
<div>
<p>The stored procedure is very simple and can archive the same result by SQL query. We use it only for demonstrating how cursors work.</p>
<p>We use a cursor for <em>products</em> table and loop though the products result set. If the quantity in stock of a product is less than 100, we log it into to a temporary table and after the loop we select all products to print it on screen.</p>
<p>Remember you must declare cursor first and then declare a NOT FOUND handler; otherwise you will get an error.</p>
<p>Wonder how Oracle <a title="PL/SQL Cursor Tutorial" href="http://www.plsqltutorial.com/plsql-cursor/">PL/SQL cursor</a> works? Check it out <a title="PL/SQL Cursor" href="http://www.plsqltutorial.com/plsql-cursor/">PL/SQL cursor tutorial</a> to have a better understanding.</p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chanpi.wordpress.com/233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chanpi.wordpress.com/233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chanpi.wordpress.com/233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chanpi.wordpress.com/233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chanpi.wordpress.com/233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chanpi.wordpress.com/233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chanpi.wordpress.com/233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chanpi.wordpress.com/233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chanpi.wordpress.com/233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chanpi.wordpress.com/233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chanpi.wordpress.com/233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chanpi.wordpress.com/233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chanpi.wordpress.com/233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chanpi.wordpress.com/233/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=233&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chanpi.wordpress.com/2011/10/18/sql-cursor-in-stored-procedures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f8f1f15fc18223e66832ac9e47e6b3e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chanthou.nget</media:title>
		</media:content>
	</item>
		<item>
		<title>Loop In Store Procedure</title>
		<link>http://chanpi.wordpress.com/2011/10/18/loop-in-store-procedure/</link>
		<comments>http://chanpi.wordpress.com/2011/10/18/loop-in-store-procedure/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 06:33:43 +0000</pubDate>
		<dc:creator>chanthou.nget</dc:creator>
				<category><![CDATA[SQL Stored Procedures]]></category>

		<guid isPermaLink="false">http://chanpi.wordpress.com/?p=231</guid>
		<description><![CDATA[Summary: In this tutorial, you will learn how to use various loop statements including WHILE, REPEAT and LOOP to run a block of code repeatedly in MySQL. MySQL stored programming language supports loop which allows you to process commands iteratively. The standard loops are discuss as follows WHILE loop The syntax of while loop is as follows: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=231&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<p><strong>Summary</strong><em>: In this tutorial, you will learn how to use various loop statements including WHILE, REPEAT and LOOP to run a block of code repeatedly in MySQL.</em></p>
<p>MySQL stored programming language supports loop which allows you to process commands iteratively. The standard loops are discuss as follows</p>
<h2>WHILE loop</h2>
<p>The syntax of while loop is as follows:</p>
</div>
<div id="highlighter_506773">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>WHILE expression DO</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>   </code><code>Statements</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>END</code> <code>WHILE</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<p>First the <em>while loop</em> checks the expression, if it is true it will executes statement until the expression become false. Because <em>while loop</em> checks the expression before <em>statements</em> executed, it is often known as pretest loop. Here is an example of using <em>while loop</em> in stored procedure:</p>
</div>
<div id="highlighter_591348">
<div>
<div>
<pre>DELIMITER $$
DROP PROCEDURE IF EXISTS WhileLoopProc$$
CREATE PROCEDURE WhileLoopProc()
      BEGIN
              DECLARE x  INT;
              DECLARE str  VARCHAR(255);
              SET x = 1;
              SET str =  '';
              WHILE x  &lt;= 5 DO
                          SET  str = CONCAT(str,x,',');
                          SET  x = x + 1;
              END WHILE;
              SELECT str;
      END$$
  DELIMITER ;</pre>
<p><span style="font-size:small;"><span class="Apple-style-span" style="line-height:normal;"><br />
</span></span></div>
</div>
</div>
<div>
<p>In stored procedures above, we build string repeatedly until the variable x greater than 5 and then we output the built string into console screen by using SELECT statement. One of common trap almost developers encounter is if the variable x is not initialized, its default value is NULL so the condition in while loop is always true; the code block inside while loop is executed indefinitively<strong> </strong>until your database server crashed.</p>
<h2>REPEAT loop</h2>
<p>The syntax of repeat loop is as follows:</p>
</div>
<div id="highlighter_692948">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>REPEAT</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>Statements;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>UNTIL expression</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>END</code> <code>REPEAT</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<p>First the <em>statements</em> are executed, and then the <em>expression</em> is evaluated. If the <em>expression</em> is evaluated as true the statements are executed again and again until its value become false. Because the <em>repeat loop</em> checks the <em>expression</em>after the execution of <em>statements</em> so it is also known as post-test loop. We can rewrite the stored procedure above by using repeat loop as follows:</p>
</div>
<div id="highlighter_543995">
<div>
<div>
<pre>DELIMITER $$
DROP PROCEDURE IF EXISTS RepeatLoopProc$$
CREATE PROCEDURE RepeatLoopProc()
      BEGIN
              DECLARE x  INT;
              DECLARE str  VARCHAR(255);
              SET x = 1;
              SET str =  '';
              REPEAT
                          SET  str = CONCAT(str,x,',');
                          SET  x = x + 1;
              UNTIL x  &gt; 5
              END REPEAT;
              SELECT str;
      END$$
DELIMITER ;</pre>
<p><span style="font-family:monospace;"><span class="Apple-style-span" style="font-size:11px;line-height:normal;"><br />
</span></span></div>
</div>
</div>
<div>
<p>Be noted that there is no delimiter (;) after <em>UNTIL expression</em></p>
<h2>LOOP loop, LEAVE and ITERATE</h2>
<p>Leave statement allows you to leave the loop. It is a bit like <em>break</em> in other languages such as Java, C#&#8230;<br />
Iterate statement allows you to start the loop again. It is like <em>continue </em>in Java or C#.<br />
MySQL also supports a LOOP loop which allows you to execute statements repeatedly and more flexible. Here is an example of using LOOP loop.</p>
</div>
<div id="highlighter_275624">
<div>
<div>
<pre>DELIMITER $$
DROP PROCEDURE IF EXISTS LOOPLoopProc$$
CREATE PROCEDURE LOOPLoopProc()
      BEGIN
              DECLARE x  INT;
              DECLARE str  VARCHAR(255);
              SET x = 1;
              SET str =  '';
              loop_label:  LOOP
                          IF  x &gt; 10 THEN
                                      LEAVE  loop_label;
                          END  IF;
                          SET  x = x + 1;
                          IF  (x mod 2) THEN
                                      ITERATE  loop_label;
                          ELSE
                                      SET  str = CONCAT(str,x,',');
                          END  IF;

              END LOOP;
              SELECT str;
      END$$
DELIMITER ;</pre>
</div>
<div>The stored procedure only constructs string with even numbers. First we define a loop label, if a variable x is greater than 10 the loop is ended because of leave statement. Otherwise if the variable x is odd, the ITERATE ignores everything below it and continues, if the variable x is even, the block after ELSE constructs strings with even numbers.</div>
</div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chanpi.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chanpi.wordpress.com/231/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chanpi.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chanpi.wordpress.com/231/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chanpi.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chanpi.wordpress.com/231/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chanpi.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chanpi.wordpress.com/231/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chanpi.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chanpi.wordpress.com/231/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chanpi.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chanpi.wordpress.com/231/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chanpi.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chanpi.wordpress.com/231/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=231&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chanpi.wordpress.com/2011/10/18/loop-in-store-procedure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f8f1f15fc18223e66832ac9e47e6b3e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chanthou.nget</media:title>
		</media:content>
	</item>
		<item>
		<title>IF Statement</title>
		<link>http://chanpi.wordpress.com/2011/10/18/if-statement/</link>
		<comments>http://chanpi.wordpress.com/2011/10/18/if-statement/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 06:31:18 +0000</pubDate>
		<dc:creator>chanthou.nget</dc:creator>
				<category><![CDATA[SQL Stored Procedures]]></category>

		<guid isPermaLink="false">http://chanpi.wordpress.com/?p=229</guid>
		<description><![CDATA[Conditional control enables you to execute the code based on the value of an expression or a combination of expression using logical operators. MySQL supports two conditional control statement such as IF and CASE. The IF Statement The syntax of IF statement is simple as follows: 1 IF expression THEN commands 2    [ELSEIF expression THEN commands] 3    [ELSE commands] 4    END IF; The commands associated with [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=229&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<p>Conditional control enables you to execute the code based on the value of an expression or a combination of expression using logical operators. MySQL supports two conditional control statement such as IF and CASE.</p>
<h2>The IF Statement</h2>
<p>The syntax of IF statement is simple as follows:</p>
</div>
<div id="highlighter_898463">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>IF expression </code><code>THEN</code> <code>commands</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>   </code><code>[ELSEIF expression </code><code>THEN</code> <code>commands]</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>   </code><code>[</code><code>ELSE</code> <code>commands]</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>   </code><code>END</code> <code>IF;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<p>The <em>commands</em> associated with IF or ELSEIF or ELSE only executed when the expression is evaluated as TRUE. One of the common trap of IF statement is NULL value; When the expression is evaluated as NULL it is neither TRUE nor FALSE. Here are several combination of IF statement</p>
</div>
<div id="highlighter_14144">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>IF expression </code><code>THEN</code> <code>commands</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>   </code><code>END</code> <code>IF;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div id="highlighter_465553">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>IF expression </code><code>THEN</code> <code>commands</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>   </code><code>ELSE</code> <code>commands</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>   </code><code>END</code> <code>IF;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div id="highlighter_902912">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>IF expression </code><code>THEN</code> <code>commands</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>   </code><code>ELSEIF expression </code><code>THEN</code> <code>commands</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>   </code><code>ELSE</code> <code>commands</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>   </code><code>END</code> <code>IF;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<p>You can have IF statement nested with other IF statements.</p>
<h2>The CASE Statement</h2>
<p>When multiple conditions are used with IF statement the code is not easy to read. At this time, the CASE can be used to make the code clearer. The syntax of  the CASE statement is as follows:</p>
</div>
<div id="highlighter_14545">
<div></div>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>CASE</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>   </code><code>WHEN</code> <code>expression </code><code>THEN</code> <code>commands</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>   </code><code>…</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>   </code><code>WHEN</code> <code>expression </code><code>THEN</code> <code>commands</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>   </code><code>ELSE</code> <code>commands</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>6</code></td>
<td><code>   </code><code>END</code> <code>CASE</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chanpi.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chanpi.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chanpi.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chanpi.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chanpi.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chanpi.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chanpi.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chanpi.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chanpi.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chanpi.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chanpi.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chanpi.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chanpi.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chanpi.wordpress.com/229/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=229&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chanpi.wordpress.com/2011/10/18/if-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f8f1f15fc18223e66832ac9e47e6b3e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chanthou.nget</media:title>
		</media:content>
	</item>
		<item>
		<title>Stored Procedure Parameters</title>
		<link>http://chanpi.wordpress.com/2011/10/18/stored-procedure-parameters/</link>
		<comments>http://chanpi.wordpress.com/2011/10/18/stored-procedure-parameters/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 06:17:57 +0000</pubDate>
		<dc:creator>chanthou.nget</dc:creator>
				<category><![CDATA[SQL Stored Procedures]]></category>

		<guid isPermaLink="false">http://chanpi.wordpress.com/?p=222</guid>
		<description><![CDATA[Summary: In this tutorial, you will learn how to write MySQL stored procedures with parameters. We will also give you a couple of stored procedure examples to help you understand more about using parameters in stored procedures. Almost stored procedures you develop require parameters. Parameters make the stored procedure more flexible and useful. In MySQL, a parameter has [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=222&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<p><strong>Summary</strong>: <em>In this tutorial, you will learn how to write MySQL stored procedures with parameters. We will also give you a couple of stored procedure examples to help you understand more about using parameters in stored procedures.</em></p>
<p>Almost stored procedures you develop require parameters. Parameters make the stored procedure more flexible and useful. In MySQL, a parameter has one of three modes IN, OUT and INOUT.</p>
<ul type="square">
<li>IN this is the default mode. IN indicates that a parameter can be passed into stored procedures but any modification inside stored procedure does not change parameter. Suppose you pass parameter <em>Id,</em> which is equal 10, into stored procedure <em>GetAll(Id)</em>, after executing the stored procedure the value of Id is still 10 even though the <em>GetAll</em> stored procedure can change the value of it.</li>
<li>OUT this mode indicates that stored procedure can change this parameter and pass back to the calling program.</li>
<li>INOUT obviously this mode is combined of IN and OUT mode; you can pass parameter into stored procedure and get it back with the new value from calling program.</li>
</ul>
<p>The syntax of defining a parameter in stored procedure is as follows:</p>
<p>MODE param_name param_type(param_size)</p>
<p>MODE could be IN, OUT or INOUT depending on the purpose of parameter you specified.<br />
param_name is the name of the parameter. The name must not be the same as the column name of tables and following naming convention. Followed the parameter name is the type of parameter and its size.</p>
<p>Each parameter is separated by a comma if the stored procedure more than one parameter.</p>
<p>Let’s practice with following examples to understand more.</p>
<p>The first example is a stored procedure to get all offices in a country. Here is the SQL source code:</p>
</div>
<div id="highlighter_533676">
<div>
<div>
<pre>DELIMITER //
 CREATE PROCEDURE GetOfficeByCountry(IN countryName VARCHAR(255))
    BEGIN
		SELECT city, phone
		FROM offices
		WHERE country = countryName;
    END //
 DELIMITER ;</pre>
<p><span style="font-size:small;"><span class="Apple-style-span" style="line-height:normal;"><br />
</span></span></p>
</div>
</div>
</div>
<div>
<p>As you can see we use countryName as the IN parameter with its type is varchar and its size is 255. In body part of stored procedure, we retrieve all offices which its country is the <em>countryName.</em><br />
Suppose you want to retrieve all office in USA, just pass the value to the stored procedures like this:</p>
</div>
<div id="highlighter_388350">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>CALL GetOfficeByCountry(</code><code>'USA'</code><code>) </code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<p>It returns all offices in USA</p>
<p>To get all offices in France just pass France to the stored procedure as follows:</p>
</div>
<div id="highlighter_199094">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>CALL GetOfficeByCountry(‘France’) </code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<p>It returns all offices in France.</p>
<p>The second example, we will write a store procedure to count the order in a specific order status such as shipped, resolved, cancelled, on hold, disputed or in process. Here is the stored procedure</p>
</div>
<div id="highlighter_574575">
<div>
<div>
<pre>DELIMITER $$
CREATE PROCEDURE CountOrderByStatus(
		IN orderStatus VARCHAR(25),
		OUT total INT)
    BEGIN
		SELECT count(orderNumber)
		INTO total
		FROM orders
		WHERE status = orderStatus;
    END$$
 DELIMITER ;</pre>
<p><span style="font-size:small;"><span class="Apple-style-span" style="line-height:normal;"><br />
</span></span></p>
</div>
</div>
</div>
<div>
<p>The CountOrderByStatus stored procedure has two parameters:</p>
<ul type="square">
<li>orderStatus parameter is IN parameter; we pass order status such as shipped or on hold in to get the number of it</li>
<li>total parameter is the OUT parameter which we use to get the total order by a specified status back.</li>
</ul>
<p>So to get number of shipped orders, we just perform following statements</p>
</div>
<div id="highlighter_110056">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>CALL  CountOrderByStatus(</code><code>'Shipped'</code><code>,@total);</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<p>To get number of in process we do the same as above</p>
</div>
<div id="highlighter_480215">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>CALL CountOrderByStatus(</code><code>'in  process'</code><code>,@total);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>SELECT</code> <code>@total </code><code>AS</code>  <code>total_in_process; </code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<p>In the third procedure, we will demonstrate the INOUT parameter. The stored procedure capitalizes all words in a string and returns it back to the calling program. The stored procedure source code is as follows:</p>
</div>
<div id="highlighter_949119">
<div>
<div>
<pre>DELIMITER $$
CREATE PROCEDURE `Capitalize`(INOUT str VARCHAR(1024))
BEGIN
	DECLARE i INT DEFAULT 1;
	DECLARE myc, pc CHAR(1);
 	DECLARE outstr VARCHAR(1000) DEFAULT str;
 	WHILE i &lt;= CHAR_LENGTH(str) DO
		SET myc = SUBSTRING(str, i, 1);
		SET pc = CASE WHEN i = 1 THEN ' '
			      ELSE SUBSTRING(str, i - 1, 1)
			 END;
		IF pc IN (' ', '&amp;', '''', '_', '?', ';', ':', '!', ',', '-', '/', '(', '.') THEN
		    SET outstr = INSERT(outstr, i, 1, UPPER(myc));
		END IF;
		SET i = i + 1;
	END WHILE;
	SET str = outstr;
END$$
DELIMITER ;</pre>
<p><span style="font-family:monospace;"><span class="Apple-style-span" style="font-size:11px;line-height:normal;"><br />
</span></span></p>
</div>
</div>
</div>
<div>
<p>Here is the usage of the Capitalize stored procedure</p>
</div>
<div id="highlighter_809286">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>SET</code> <code>@str = </code><code>'mysql stored procedure tutorial'</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>CALL Capitalize(@str);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>SELECT</code> <code>@str;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<p>And the @str value is ‘Mysql Stored Procedure Tutorial’</p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chanpi.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chanpi.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chanpi.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chanpi.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chanpi.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chanpi.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chanpi.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chanpi.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chanpi.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chanpi.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chanpi.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chanpi.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chanpi.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chanpi.wordpress.com/222/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chanpi.wordpress.com&amp;blog=11662408&amp;post=222&amp;subd=chanpi&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chanpi.wordpress.com/2011/10/18/stored-procedure-parameters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f8f1f15fc18223e66832ac9e47e6b3e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chanthou.nget</media:title>
		</media:content>
	</item>
	</channel>
</rss>
