<?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>MySQL Preacher &#187; stored procedure</title>
	<atom:link href="http://mysqlpreacher.com/wordpress/tag/stored-procedure/feed/" rel="self" type="application/rss+xml" />
	<link>http://mysqlpreacher.com/wordpress</link>
	<description>Because Sharing is Caring</description>
	<lastBuildDate>Sat, 14 Apr 2012 17:45:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Getting a return code from a stored procedure</title>
		<link>http://mysqlpreacher.com/wordpress/2010/08/getting-a-return-code-from-a-stored-procedure/</link>
		<comments>http://mysqlpreacher.com/wordpress/2010/08/getting-a-return-code-from-a-stored-procedure/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 06:00:20 +0000</pubDate>
		<dc:creator>Darren Cassar</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[stored procedure]]></category>
		<category><![CDATA[stored procedures]]></category>

		<guid isPermaLink="false">http://mysqlpreacher.com/wordpress/?p=430</guid>
		<description><![CDATA[Sometimes we have some special need for a stored procedure to call another to do something. That is fine, but what if the second stored proc failed for some reason? Maybe you want to halt the first stored procedure (the caller) and not proceed with the work until the problem is verified and resolved. So [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes we have some special need for a stored procedure to call another to do something. That is fine, but what if the second stored proc failed for some reason? Maybe you want to halt the first stored procedure (the caller) and not proceed with the work until the problem is verified and resolved. So How do you do it?</p>
<p>Simple, get a return code from the called procedure to determine if it worked or not!</p>
<p>Here&#8217;s a sample piece of code to explain better:</p>
<p><code><br />
DROP PROCEDURE IF EXISTS `test`.`testing123`;<br />
DELIMITER $$</p>
<p>CREATE<br />
    PROCEDURE `test`.`testing123`(OUT a INT)<br />
    BEGIN<br />
        DECLARE EXIT HANDLER FOR SQLEXCEPTION<br />
        BEGIN<br />
          SET a=2;<br />
        END;<br />
        SET a=0;</p>
<p># toggle the below as comment or not to see the call at the bottom working<br />
# if you uncomment select abc you'll see the error, otherwise all is cool</p>
<p>#        select abc;</p>
<p>    END$$</p>
<p>DELIMITER ;</p>
<p>DROP PROCEDURE IF EXISTS `test`.`testing456`;<br />
DELIMITER $$</p>
<p>CREATE<br />
    PROCEDURE `test`.`testing456`()<br />
    BEGIN<br />
        SET @a=0;<br />
        CALL `test`.`testing123` (@a);</p>
<p>        IF @a != 0 THEN<br />
           SELECT "There is a problem with proc `testing123`";<br />
        ELSE<br />
           SELECT "Everything is cool";<br />
        END IF;</p>
<p>    END$$</p>
<p>DELIMITER ;</p>
<p>CALL `test`.`testing456` ();<br />
</code></p>
<p>testing123 is the second stored proc in this case, called from testing456. The trick is to have an exit handler which returns a status number to the first stored proc (testing456). The latter will hold an if condition to do `something` depending on the return code received by testing123.</p>
<p>If you have any other suggestions I&#8217;d appreciate your input.</p>
]]></content:encoded>
			<wfw:commentRss>http://mysqlpreacher.com/wordpress/2010/08/getting-a-return-code-from-a-stored-procedure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL stored procedure debugging, can I sue for going insane?</title>
		<link>http://mysqlpreacher.com/wordpress/2010/08/mysql-stored-procedure-debugging-can-i-sue-for-going-insane/</link>
		<comments>http://mysqlpreacher.com/wordpress/2010/08/mysql-stored-procedure-debugging-can-i-sue-for-going-insane/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 15:10:28 +0000</pubDate>
		<dc:creator>Darren Cassar</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[mysq]]></category>
		<category><![CDATA[procs]]></category>
		<category><![CDATA[stored procedure]]></category>
		<category><![CDATA[stored procs]]></category>

		<guid isPermaLink="false">http://mysqlpreacher.com/wordpress/?p=423</guid>
		<description><![CDATA[Lets paint the picture: Scenario part 1 : Migrating a couple thousand stored procedures from database technology X to mysql Scenario part 2 : Legacy system where the people who wrote it left a long time ago Scenario part 3 : Developers sure can get real creative and invent all kinds of ways to get [...]]]></description>
			<content:encoded><![CDATA[<p>Lets paint the picture:</p>
<p>Scenario part 1 : Migrating a couple thousand stored procedures from database technology X to mysql<br />
Scenario part 2 : Legacy system where the people who wrote it left a long time ago<br />
Scenario part 3 : Developers sure can get real creative and invent all kinds of ways to get data (eg: having a stored proc which formulates a big query using concat after going through a bunch of conditions (fair enough), BUT the parts making up the different queries are stored in a table on a database rather than within the stored proc itself) &#8230; talk about KIS &#8211; Keep it simple!!<br />
Scenario part 4 : This stored proc references 18 tables, 4 views, and another two stored procedures on 5 databases</p>
<p>Now close your eyes and try to imagine that for a few seconds, nah kidding don&#8217;t want you to hurt yourself.</p>
<p>I wonder, who&#8217;s gonna cover my health insurance if i go crazy? :)</p>
<p>mysql 02:55:47 DEV > call storedprocblahbla(&#8216;I&#8217;,'am&#8217;,'going&#8217;,'crazy&#8217;);<br />
ERROR 1052 (23000): Column &#8216;state_of_mind&#8217; in field list is ambiguous</p>
<p>Sure thats REALLY REALLY helpful thanks :), you know what, lets just mysqldump -d -R -B db1 db2 db3 db4 db5 > /wherever/you/like and grep for column &#8216;state_of_mind&#8217;.</p>
<p>I love a challenge but facing migration of so many stored procs I&#8217;d really love to have something &#8220;GOOD&#8221; to debug them with.</p>
<p>The solutions I found were:<br />
1. Illatis eclipse plugin for $40<br />
	works on linux, mac and windows but does show me a lot of:<br />
	&#8220;An error has occurred when activating this view<br />
	com/illatis/parser/lib/k&#8221;<br />
2. mydebugger for $50<br />
	works on linux and mac via wine<br />
3. dbForge for MySQL for $50<br />
	works only on windows due to .net framework (kinda crap because I `hate` using the former)</p>
<p>any other ideas are very welcome, especially opensource ones but I can&#8217;t find anything.</p>
<p>I can always try setting up debugging using GDB but would rather try to avoid that.</p>
]]></content:encoded>
			<wfw:commentRss>http://mysqlpreacher.com/wordpress/2010/08/mysql-stored-procedure-debugging-can-i-sue-for-going-insane/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MySQL processlist &#8211; (show/kill processes)</title>
		<link>http://mysqlpreacher.com/wordpress/2009/07/mysql-processlist-showkill-processes/</link>
		<comments>http://mysqlpreacher.com/wordpress/2009/07/mysql-processlist-showkill-processes/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 10:55:41 +0000</pubDate>
		<dc:creator>Darren Cassar</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[kill]]></category>
		<category><![CDATA[mysql stored procedure]]></category>
		<category><![CDATA[processes]]></category>
		<category><![CDATA[processlist]]></category>
		<category><![CDATA[sp]]></category>
		<category><![CDATA[stored proc]]></category>
		<category><![CDATA[stored procedure]]></category>

		<guid isPermaLink="false">http://mysqlpreacher.com/wordpress/?p=208</guid>
		<description><![CDATA[It"s not the most common task in the world, but you might want to view processes from a particular user and once in a while you might even need to kill processes from a single user, be it during an attack or because you simply got a bug in an application bombarding your db server with connections!]]></description>
			<content:encoded><![CDATA[<p>It&#8221;s not the most common task in the world, but you might want to view processes from a particular user and once in a while you might even need to kill processes from a single user, be it during an attack or because you simply got a bug in an application bombarding your db server with connections!</p>
<p>Here is a small stored procedure which does exactly that!</p>
<blockquote><p><code lang="sql">call process_list("show","username","hostname");</code><br />
&#8211; shows all processes owned by username@hostname<br />
<code lang="sql">call process_list("kill","username","hostname");</code><br />
&#8211; kills all processes owned by username@hostname</p></blockquote>
<p>The code for this stored procedure can be found below. If you have any comments / suggestions feel free to comment below.</p>
<blockquote><p><code lang="sql">######################################################################<br />
##                                                                  ##<br />
##  Stored Procedure: process_list &amp; kill user                  ##<br />
##  call process_list("show","%") show processlist for all users    ##<br />
##  call process_list("show","root") show processlist for root user ##<br />
##  call process_list("kill","user1") kill connections for user1    ##<br />
##                                                                  ##<br />
##  by Darren Cassar http://www.mysqlpreacher.com                   ##<br />
##                                                                  ##<br />
######################################################################</code></p>
<p><code lang="sql">DROP PROCEDURE IF EXISTS process_list;</code></p>
<p><code lang="sql">DELIMITER $$</code></p>
<p><code lang="sql">CREATE PROCEDURE "process_list"( choice char(4), usernamein varchar(16), hostnamein varchar(60))<br />
BEGIN</p>
<p>DECLARE CURCONN int;</p>
<p>IF choice &lt;&gt; "show" AND choice &lt;&gt; "kill" then<br />
select "wrong choice";<br />
END IF;</p>
<p>IF usernamein = "" then<br />
set usernamein = "%";<br />
END IF;</p>
<p>IF hostnamein = "" then<br />
set hostnamein = "%";<br />
END IF;</p>
<p>SET CURCONN=(select connection_id());</p>
<p>IF choice = "show" then</p>
<p>select *<br />
from information_schema.processlist<br />
where ID &lt;&gt; CURCONN and<br />
USER like usernamein and<br />
( HOST like CONCAT(hostnamein ,":%") or<br />
HOST like hostnamein );</p>
<p>ELSEIF choice = "kill" then</p>
<p>IF usernamein = "root" then<br />
select "Illegal username when killing processes";<br />
ELSE<br />
SET @CNT = (<br />
select count(*)<br />
from information_schema.processlist<br />
where ID &lt;&gt; CURCONN and<br />
USER like usernamein and<br />
( HOST like CONCAT(hostnamein ,":%") or<br />
HOST like hostnamein )<br />
);</p>
<p>SET @VAR=1;</p>
<p>WHILE ( @VAR &lt;= @CNT) DO</p>
<p>SET @TID = (<br />
select id<br />
from information_schema.processlist<br />
where ID &lt;&gt; CURCONN and<br />
USER like usernamein and<br />
( HOST like CONCAT(hostnamein ,":%") or<br />
HOST like hostnamein ) limit 1<br />
);</p>
<p>SET @k = CONCAT("kill " , @TID);<br />
PREPARE killcom FROM @k;<br />
EXECUTE killcom;<br />
set @k=NULL;</p>
<p>SET @VAR=@VAR+1;</p>
<p>END WHILE;</p>
<p>END IF;</p>
<p>END IF;</p>
<p>END$$</p>
<p></code><code lang="sql">DELIMITER ;<br />
</code></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://mysqlpreacher.com/wordpress/2009/07/mysql-processlist-showkill-processes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

