<?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>youdidwhatwithtsql.com &#187; sp_MsForEachTale</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/sp_msforeachtale/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +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>MySQL clone of sp_msforeachtable</title>
		<link>http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_msforeachtable/624</link>
		<comments>http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_msforeachtable/624#comments</comments>
		<pubDate>Sat, 13 Feb 2010 19:56:47 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[sp_MsForEachTale]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_msforeachtable/624</guid>
		<description><![CDATA[Many SQL Server DBAs and Developers get a  lot of use out of the undocumented sp_MsForEachTable system stored procedure. Here&#8217;s an attempt at creating a functional version for MySQL. The procedure makes use of prepared statements, which do have some limitations, so some uses may not translate across. This is far from production ready so [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_msforeachtable/624">MySQL clone of sp_msforeachtable</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Many <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> DBAs and Developers get a  lot of use out of the <a href="http://connect.microsoft.com/SQLServer/feedback/details/264676/sp-msforeachtable-provide-supported-documented-version" target="_blank">undocumented sp_MsForEachTable</a> system stored procedure. Here&#8217;s an attempt at creating a functional version for <a href="http://www.mysql.com" target="_blank">MySQL</a>. The procedure makes use of <a href="http://rpbouman.blogspot.com/2005/11/mysql-5-prepared-statement-syntax-and.html" target="_blank">prepared statements</a>, which do have some limitations, so some uses may not translate across. This is far from production ready so use with lots of caution.</p>
<p>Create the below stored procedure in the database you wish to use it in.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p624code7'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6247"><td class="code" id="p624code7"><pre class="mysql" style="font-family:monospace;">DELIMITER $$
&nbsp;
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> <span style="color: #008000;">`usp<span style="color: #008080; font-weight: bold;">_</span>mysql<span style="color: #008080; font-weight: bold;">_</span>foreachtable`</span>$$
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #008000;">`usp<span style="color: #008080; font-weight: bold;">_</span>mysql<span style="color: #008080; font-weight: bold;">_</span>foreachtable`</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">IN</span> sql_string <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">1000</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">LANGUAGE SQL</span>
    <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #990099; font-weight: bold;">DETERMINISTIC</span>
    <span style="color: #990099; font-weight: bold;">COMMENT</span> <span style="color: #008000;">'Functional clone of sp<span style="color: #008080; font-weight: bold;">_</span>MsForEachTable'</span>
    <span style="color: #990099; font-weight: bold;">BEGIN</span>
&nbsp;
	<span style="color: #990099; font-weight: bold;">DECLARE</span> var_tablename <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">DECLARE</span> last_row <span style="color: #999900; font-weight: bold;">BIT</span><span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #990099; font-weight: bold;">DECLARE</span> table_cursor CURSOR FOR <span style="color: #990099; font-weight: bold;">SELECT</span> TABLE_NAME
					<span style="color: #990099; font-weight: bold;">FROM</span> information_schema.<span style="color: #990099; font-weight: bold;">TABLES</span>
					<span style="color: #990099; font-weight: bold;">WHERE</span> TABLE_TYPE <span style="color: #CC0099;">=</span> <span style="color: #008000;">'BASE TABLE'</span>
					<span style="color: #CC0099; font-weight: bold;">AND</span> TABLE_SCHEMA <span style="color: #CC0099;">=</span> <span style="color: #000099;">DATABASE</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #990099; font-weight: bold;">DECLARE</span> CONTINUE <span style="color: #990099; font-weight: bold;">HANDLER</span> FOR <span style="color: #CC0099; font-weight: bold;">NOT</span> FOUND <span style="color: #990099; font-weight: bold;">SET</span> last_row <span style="color: #CC0099;">=</span> <span style="color: #008080;">1</span><span style="color: #000033;">;</span>
&nbsp;
	OPEN table_cursor<span style="color: #000033;">;</span>
	FETCH table_cursor <span style="color: #990099; font-weight: bold;">INTO</span> var_tablename<span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #990099; font-weight: bold;">SET</span> last_row <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">SET</span> @var <span style="color: #CC0099;">=</span> <span style="color: #008000;">''</span><span style="color: #000033;">;</span>
&nbsp;
	lbl_table_cursor: LOOP
&nbsp;
		<span style="color: #990099; font-weight: bold;">SET</span> @qry <span style="color: #CC0099;">=</span> <span style="color: #000099;">REPLACE</span><span style="color: #FF00FF;">&#40;</span>sql_string<span style="color: #000033;">,</span> <span style="color: #008000;">'?'</span><span style="color: #000033;">,</span> var_tablename<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
		PREPARE q <span style="color: #990099; font-weight: bold;">FROM</span> @qry<span style="color: #000033;">;</span>
		EXECUTE q<span style="color: #000033;">;</span>
		DEALLOCATE PREPARE q<span style="color: #000033;">;</span>
&nbsp;
		FETCH table_cursor <span style="color: #990099; font-weight: bold;">INTO</span> var_tablename<span style="color: #000033;">;</span>
		<span style="color: #009900;">IF</span> last_row <span style="color: #CC0099;">=</span> <span style="color: #008080;">1</span> <span style="color: #009900;">THEN</span>
			LEAVE lbl_table_cursor<span style="color: #000033;">;</span>
		<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span> LOOP lbl_table_cursor<span style="color: #000033;">;</span>
&nbsp;
	CLOSE table_cursor<span style="color: #000033;">;</span>
&nbsp;
    <span style="color: #009900;">END</span>$$
&nbsp;
DELIMITER <span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Usage is quite simple, just pass in a query with a &#8216;?&#8217; in place of where the table name should be. I have verified the below use cases function as expected.</p>
<p><strong>Select 5 rows from each table</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p624code8'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6248"><td class="code" id="p624code8"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CALL</span> usp_mysql_foreachtable<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'SELECT * FROM ? LIMIT 5;'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><strong>Count the number of rows in each table</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p624code9'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6249"><td class="code" id="p624code9"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CALL</span> usp_mysql_foreachtable<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'SELECT '</span><span style="color: #008000;">'?'</span><span style="color: #008000;">', COUNT(*) AS Rows FROM ?'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><strong>Repair all tables in the database</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p624code10'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p62410"><td class="code" id="p624code10"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CALL</span> usp_mysql_foreachtable<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'REPAIR TABLE ?'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><strong>Change all tables to use the InnoDb storage engine</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p624code11'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p62411"><td class="code" id="p624code11"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CALL</span> usp_mysql_foreachtable<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'ALTER TABLE ? ENGINE=InnoDB'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><strong>Reset the AUTO_INCREMENT to zero in all tables</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p624code12'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p62412"><td class="code" id="p624code12"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CALL</span> usp_mysql_foreachtable<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'ALTER TABLE ? AUTO<span style="color: #008080; font-weight: bold;">_</span>INCREMENT = 0'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><map name='google_ad_map_624_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/624?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_624_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=624&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fmysql-clone-of-sp_msforeachtable%2F624' title="MySQL clone of sp msforeachtable" alt=" MySQL clone of sp msforeachtable" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_msforeachtable/624">MySQL clone of sp_msforeachtable</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_msforeachtable/624" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_msforeachtable/624/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

