<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>D-talk - MySQL</title>
    <link>http://blog.dynom.nl/</link>
    <description>The little things that pop into mind</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.2-beta4 - http://www.s9y.org/</generator>
    
    

<item>
    <title>Drop empty columns with dynamically defined columns and tables</title>
    <link>http://blog.dynom.nl/archives/Drop-empty-columns-with-dynamically-defined-columns-and-tables_20100817_58.html</link>
            <category>MySQL</category>
    
    <comments>http://blog.dynom.nl/archives/Drop-empty-columns-with-dynamically-defined-columns-and-tables_20100817_58.html#comments</comments>
    <wfw:comment>http://blog.dynom.nl/wfwcomment.php?cid=58</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://blog.dynom.nl/rss.php?version=2.0&amp;type=comments&amp;cid=58</wfw:commentRss>
    

    <author>nospam@example.com (Mark van der Velden)</author>
    <content:encoded>
    &lt;p&gt;For a migration process I wanted to build in extra validation in some destructive MySQL queries, to eliminate the risk that data might be lost. Of course all data is back-upped, but I rather be safe then sorry. &lt;/p&gt;&lt;p&gt;This is fairly straight forward, create a stored procedure and perform an ALTER statement whenever the previously defined criteria have been met. But I wanted to define a single procedure rather then create one for every table I had to drop columns from. And since you can&#039;t simply use variables for column/table names, you have to improvise a little. I came up with the following:&lt;/p&gt;&lt;p&gt;&lt;div class=&quot;sql&quot; style=&quot;text-align: left&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;-- Defining the &amp;quot;drop empty column&amp;quot; SP&lt;/span&gt;&lt;br /&gt;DELIMITER @@&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;DROP&lt;/span&gt; PROCEDURE &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;IF&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;EXISTS&lt;/span&gt; drop_empty_column @@&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;CREATE&lt;/span&gt; PROCEDURE drop_empty_column&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;IN&lt;/span&gt; itable VARCHAR&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;IN&lt;/span&gt; icolumn VARCHAR&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; OUT succeeded INT&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;BEGIN&lt;br /&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SET&lt;/span&gt; @amount = -&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SET&lt;/span&gt; @itable = itable;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SET&lt;/span&gt; @icolumn = icolumn;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;-- Build the query, with dynamic table and column. Store the result in @amount&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt; CONCAT&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;SELECT COUNT(&#039;&lt;/span&gt;, @icolumn ,&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;) INTO @amount FROM &#039;&lt;/span&gt;, @itable ,&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039; WHERE(&#039;&lt;/span&gt;, @icolumn ,&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039; IS NOT NULL OR &#039;&lt;/span&gt;, @icolumn ,&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039; != &amp;quot;&amp;quot;);&#039;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;INTO&lt;/span&gt; @testSql;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; PREPARE testSqlStmt &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;FROM&lt;/span&gt; @testSql;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;-- Execute&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; EXECUTE testSqlStmt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;-- Check if we have 0 rows, else we still have data and we can&#039;t drop.&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;IF&lt;/span&gt; @amount = &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt; THEN&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;-- Build the query&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt; CONCAT&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;ALTER TABLE &#039;&lt;/span&gt;, @itable ,&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039; DROP COLUMN &#039;&lt;/span&gt;, @icolumn ,&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;;&#039;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;INTO&lt;/span&gt; @alterSql;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;-- Execute&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; PREPARE alterSqlStmt &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;FROM&lt;/span&gt; @alterSql;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; EXECUTE alterSqlStmt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;-- Update the status&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;INTO&lt;/span&gt; succeeded;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; ELSE&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;INTO&lt;/span&gt; succeeded;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; END &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;IF&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;END@@&lt;br /&gt;DELIMITER ;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;-- Conditionally drop the column &amp;quot;deprecated_column&amp;quot; from table &amp;quot;some_table&amp;quot;&lt;/span&gt;&lt;br /&gt;CALL drop_empty_column&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;some_table&#039;&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;deprecated_column&#039;&lt;/span&gt;, @succeeded&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;-- Show the status&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt; @succeeded;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;-- Cleanup&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;DROP&lt;/span&gt; PROCEDURE &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;IF&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;EXISTS&lt;/span&gt; drop_empty_column;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;p&gt;I&#039;m fairly positive that this could be done in a much better way then that I&#039;m doing here, but this works too. Simply repeat the CALL drop_empty_column(..) for every table/column combination you want to DROP and update the definition of an &quot;empty column&quot; to what you want. Currently it drops the column only if the values contain nothing other then NULL or &quot;&quot; (empty string) values.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Further reading:&lt;/b&gt; &lt;br /&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/dev.mysql.com/tech-resources/articles/mysql-storedproc.html%20&#039;);&quot;  href=&quot;http://dev.mysql.com/tech-resources/articles/mysql-storedproc.html%20&quot; mce_href=&quot;http://dev.mysql.com/tech-resources/articles/mysql-storedproc.html &quot;&gt;http://dev.mysql.com/tech-resources/articles/mysql-storedproc.html&amp;#160;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/dev.mysql.com/doc/refman/5.0/en/create-procedure.html&#039;);&quot;  href=&quot;http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html&quot; mce_href=&quot;http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html&quot;&gt;http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt; 
    </content:encoded>

    <pubDate>Tue, 17 Aug 2010 13:54:00 +0200</pubDate>
    <guid isPermaLink="false">http://blog.dynom.nl/archives/guid_20100817_58.html</guid>
    <category>mysql</category>
<category>prepared statement</category>
<category>variable table name</category>

</item>

</channel>
</rss>
