<?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>Rusty Razor Blade &#187; stored procedure</title>
	<atom:link href="http://www.rustyrazorblade.com/category/stored-procedure/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rustyrazorblade.com</link>
	<description>Tech Thoughts, Mostly on LAMP - by Jon Haddad</description>
	<lastBuildDate>Wed, 21 Jul 2010 20:42:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Stored Procedure For Finding Columns In MySQL</title>
		<link>http://www.rustyrazorblade.com/2009/12/stored-procedure-for-finding-columns/</link>
		<comments>http://www.rustyrazorblade.com/2009/12/stored-procedure-for-finding-columns/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 19:13:34 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[stored procedure]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=931</guid>
		<description><![CDATA[
Looking for instances particular column in a large schema can be a pain.  Fortunately the information schema makes this pretty easy, if your columns have a consistent naming convention.  

SELECT table_schema, table_name, column_name
FROM information_schema.columns
WHERE column_name LIKE '%some_name%';

Now, if we want to wrap this up into an easy to use stored procedure, we can [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.rustyrazorblade.com/wp-content/uploads/2009/12/sakila.png" alt="sakila.png" border="0" width="121" height="79" align="right" vspace = 10 hspace=10 /></p>
<p>Looking for instances particular column in a large schema can be a pain.  Fortunately the information schema makes this pretty easy, if your columns have a consistent naming convention.  </p>
<pre>
SELECT table_schema, table_name, column_name
FROM information_schema.columns
WHERE column_name LIKE '%some_name%';
</pre>
<p>Now, if we want to wrap this up into an easy to use stored procedure, we can do something like this:</p>
<pre>
drop procedure find_column;
delimiter //
CREATE PROCEDURE find_column(c varchar(255))
begin
	SET @a = CONCAT("%", c, "%");
	SELECT table_schema, table_name, column_name, column_type
		FROM information_schema.columns
		WHERE column_name LIKE @a;
end
//
delimiter ;
</pre>
<p>We need to use the concat statement in order to properly get the quotes in there without using the literal string &#8220;c&#8221; in the LIKE statement.</p>
<p>You can do a search as follows:</p>
<pre>
CALL find_column("some_column");
</pre>
<p>Learn more on the <a href="http://dev.mysql.com/doc/refman/5.1/en/stored-routines.html">MySQL Stored Procedures</a> section of <a href="http://mysql.com/">mysql.com</a></p>
<p><em>Edit: added column type on suggested by <a href="http://rpbouman.blogspot.com/">Roland Bouman</a>. </em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2009/12/stored-procedure-for-finding-columns/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
