<?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"
	>

<channel>
	<title>Trendics Blog &#187; mysql</title>
	<atom:link href="http://blog.trendics.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.trendics.com</link>
	<description></description>
	<pubDate>Mon, 11 Aug 2008 14:59:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>MySQL Stored Procedure Not Working as Expected Puzzler</title>
		<link>http://blog.trendics.com/development/mysql/mysql-stored-procedure-not-working-as-expected-puzzler/</link>
		<comments>http://blog.trendics.com/development/mysql/mysql-stored-procedure-not-working-as-expected-puzzler/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 17:26:53 +0000</pubDate>
		<dc:creator>kent</dc:creator>
		
		<category><![CDATA[mysql]]></category>

		<category><![CDATA[parameter names]]></category>

		<category><![CDATA[stored procedures]]></category>

		<guid isPermaLink="false">http://blog.trendics.com/?p=120</guid>
		<description><![CDATA[I recently spent 3 hours pulling my hair out trying to fix a MySQL stored procedure that wasn&#8217;t working as expected.  I&#8217;ve boiled the ultimate problem down to a simple example below.  Can you spot the problem?
# First, let's create some sample data
CREATE TABLE TrendicsTest (
DateTime DATETIME NOT NULL,
Value INT NOT NULL,
UNIQUE KEY [...]]]></description>
			<content:encoded><![CDATA[<p>I recently spent 3 hours pulling my hair out trying to fix a MySQL stored procedure that wasn&#8217;t working as expected.  I&#8217;ve boiled the ultimate problem down to a simple example below.  Can you spot the problem?</p>
<p style="padding-left: 30px;"><code># First, let's create some sample data<br />
CREATE TABLE TrendicsTest (<br />
DateTime DATETIME NOT NULL,<br />
Value INT NOT NULL,<br />
UNIQUE KEY DateTime (DateTime)<br />
);<br />
INSERT INTO TrendicsTest VALUES ('2008-01-01 01:00', 1);<br />
INSERT INTO TrendicsTest VALUES ('2008-01-01 02:00', 2);</p>
<p style="padding-left: 30px;"><code># Next, lets' define a stored procedure to query the sample data<br />
DELIMITER |<br />
DROP PROCEDURE IF EXISTS summarizeTrendicsTest|<br />
CREATE PROCEDURE summarizeTrendicsTest(dateTime DATETIME)<br />
BEGIN<br />
SELECT @endDateTime := DATE_ADD(dateTime, INTERVAL 1 HOUR);<br />
SELECT * FROM TrendicsTest WHERE DateTime=@endDateTime;<br />
END;<br />
|<br />
DELIMITER ;</p>
<p style="padding-left: 30px;"><code># Now, let's call the stored procedure and expect<br />
# to get back the first row of data<br />
call summarizeTrendicsTest('2008-01-01 00:00');</p>
<p>What the hell?  The first SELECT that sets @endDateTime variable echoes a result but the main SELECT returns an empty set.  Just to check our query, let's make sure the query runs ok...</p>
<p style="padding-left: 30px;"><code># Add one hour to the time just like the stored procedure<br />
SELECT * FROM TrendicsTest WHERE DateTime='2008-01-01 01:00';</p>
<p>Yes, running the query returns data so why doesn't the stored procedure return any data that appears to be running the same SELECT?  Evidently, using "dateTime" for the stored procedure param and using "DateTime" for the field in the WHERE clause confuses MySQL.  The stored procedure can be fixed by renaming the param name to something else...</p>
<p style="padding-left: 30px;"><code>DELIMITER |<br />
DROP PROCEDURE IF EXISTS summarizeTrendicsTest|<br />
CREATE PROCEDURE summarizeTrendicsTest(dateTimeXyz DATETIME)<br />
BEGIN<br />
SELECT @endDateTime := DATE_ADD(dateTimeXyz, INTERVAL 1 HOUR);<br />
SELECT * FROM TrendicsTest WHERE DateTime=@endDateTime;<br />
END;<br />
|<br />
DELIMITER ;</p>
<p style="padding-left: 30px;"><code># Now, let's call the stored procedure and expect<br />
# to get back the second row of data<br />
call summarizeTrendicsTest('2008-01-01 00:00');</p>
<p>Bah, in a complex stored procedure that is really not so easy to figure out.  So, <strong>be careful with your choice of stored procedure parameter names &#8212; avoid using a parameter name that is the same as a field name in your tables</strong>.  Seems like the MySQL stored procedure compiler should at least warn you when this happens if not throw an error.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.trendics.com/development/mysql/mysql-stored-procedure-not-working-as-expected-puzzler/feed/</wfw:commentRss>
		</item>
		<item>
		<title>MySQL Prompt Bar Charts</title>
		<link>http://blog.trendics.com/development/mysql-prompt-bar-charts/</link>
		<comments>http://blog.trendics.com/development/mysql-prompt-bar-charts/#comments</comments>
		<pubDate>Sun, 20 Jul 2008 19:09:41 +0000</pubDate>
		<dc:creator>kent</dc:creator>
		
		<category><![CDATA[development]]></category>

		<category><![CDATA[mysql]]></category>

		<category><![CDATA[mysql bar chart tip]]></category>

		<guid isPermaLink="false">http://blog.trendics.com/?p=50</guid>
		<description><![CDATA[You can generate simple bar charts directly from a MySQL prompt that look like this...

+-------+-----------------------+-----------------------------------------------+
&#124; Month &#124; Uptime                &#124; Latency                     [...]]]></description>
			<content:encoded><![CDATA[<p><code>You can generate simple bar charts directly from a MySQL prompt that look like this...</code></p>
<blockquote>
<pre>+-------+-----------------------+-----------------------------------------------+
| Month | Uptime                | Latency                                       |
+-------+-----------------------+-----------------------------------------------+
| Jan   | %%%%%%%%%%%%%%%%%%%%% | ####################                          |
| Feb   | %%%%%%%%%%%%%%%%%%%   | ##############################                |
| Mar   | %%%%%%%%%%%%%%%%%%%%  | #########################################     |
| Apr   | %%%%%%%%%%%%%%%%%%%%% | #####################                         |
| May   | %%%%%%%%%%%%%%%%%%%%% | ###############                               |
| Jun   | %%%%%%%%%%%%%%%%%%    | ############################################# |
| Jul   | %%%%%%%%%%%%%%%%%%%%% | ################################              |
| Aug   | %%%%%%%%%%%%%%%%%%%%% | ##############################                |
| Sep   | %%%%%%%%%%%%%%%%%%%%% | ###############                               |
| Oct   | %%%%%%%%%%%%%%%%%%    | ####################################          |
| Nov   | %%%%%%%%%%%%%%%%%%%%  | ##########################                    |
| Dec   | %%%%%%%%%%%%%%%%%%%%% | #################                             |
+-------+-----------------------+-----------------------------------------------+</pre>
</blockquote>
<p>To generate this type of output, you can execute the following commands to generate the sample data and to execute the SELECT that generates the bar chart itself&#8230;</p>
<blockquote>
<pre>CREATE TABLE TrendicsWebsiteCheckResults (
	Month VARCHAR(3) NOT NULL,
	Uptime FLOAT NOT NULL,
	Latency FLOAT NOT NULL
);
INSERT INTO TrendicsWebsiteCheckResults VALUES ('Jan', 100.0, 97.5),
('Feb', 92.3, 145.7),('Mar', 95.6, 201.1), ('Apr', 100.0, 101.3),
('May', 100.0, 72.0), ('Jun', 87.5, 221.0), ('Jul', 98.6, 152.7),
('Aug', 100.0, 144.8), ('Sep', 100.0, 68.8), ('Oct', 87.5, 177.2),
('Nov', 95.6, 123.4), ('Dec', 100.0, 77.9);

SELECT Month,
REPEAT("%", (Uptime-0.0)*0.2+1) Uptime,
REPEAT("#", (Latency-0.0)*0.2+1) Latency
FROM TrendicsWebsiteCheckResults;</pre>
</blockquote>
<p>Within the REPEAT() function, use whatever character you like for your bar chart and adjust the constants to scale each bar chart appropriately.  This is a super simple way to quickly chart out data from a MySQL prompt.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.trendics.com/development/mysql-prompt-bar-charts/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
