<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" 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/"><channel><title>Adventures of an aspiring agile developer in a not-quite agile world : SQL</title><link>http://blogs.msdn.com/agilemonkey/archive/tags/SQL/default.aspx</link><description>Tags: SQL</description><dc:language>en-CA</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Dropping multiple tables in SQL Server 2005</title><link>http://blogs.msdn.com/agilemonkey/archive/2007/09/19/dropping-multiple-tables-in-sql-server-2005.aspx</link><pubDate>Wed, 19 Sep 2007 09:14:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4992945</guid><dc:creator>casper</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/agilemonkey/comments/4992945.aspx</comments><wfw:commentRss>http://blogs.msdn.com/agilemonkey/commentrss.aspx?PostID=4992945</wfw:commentRss><wfw:comment>http://blogs.msdn.com/agilemonkey/rsscomments.aspx?PostID=4992945</wfw:comment><description>
&lt;p&gt;I'm currently working on a project that has a Cache database that stores query results, and so naturally there are times when I'm playing inside of Management Studio that I want to delete the cache. However, I don't want to drop the entire database (because then I'd have to re-run the create script), nor do I want to delete all the tables in it (there is one table that needs to remain).&lt;/p&gt;

&lt;p&gt;In case it's of use to anyone, or I need to do this again, here's a snippet of T-SQL that will drop an arbitrary list of tables from a database.&lt;/p&gt;

&lt;pre&gt;&lt;p&gt;DECLARE @id varchar(255) &lt;br&gt;DECLARE @dropCommand varchar(255) &lt;/p&gt;&lt;p&gt;DECLARE tableCursor CURSOR FOR &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT name FROM sys.tables WHERE name &amp;lt;&amp;gt; 'CacheManager' &lt;/p&gt;&lt;p&gt;OPEN tableCursor &lt;br&gt;FETCH next FROM tableCursor INTO @id &lt;/p&gt;&lt;p&gt;WHILE @@fetch_status=0 &lt;br&gt;BEGIN &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET @dropcommand = N'drop table ' + @id &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EXECUTE(@dropcommand) &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FETCH next FROM tableCursor INTO @id &lt;br&gt;END &lt;/p&gt;&lt;p&gt;CLOSE tableCursor &lt;br&gt;DEALLOCATE tableCursor&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;I wish there was a 'Copy as HTML' function or something similar I could use to get the nicely formatted text from Management Studio into a blog posting.&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4992945" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/agilemonkey/archive/tags/SQL/default.aspx">SQL</category></item></channel></rss>