<?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>Cranky Bit &#187; SQL</title>
	<atom:link href="http://blog.crankybit.com/tags/microsoft/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.crankybit.com</link>
	<description>Take a byte out of tech!</description>
	<lastBuildDate>Wed, 09 Mar 2011 17:23:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Shrink the Unshrinkable SQL Transaction Log</title>
		<link>http://blog.crankybit.com/shrink-the-unshrinkable-sql-transaction-log/</link>
		<comments>http://blog.crankybit.com/shrink-the-unshrinkable-sql-transaction-log/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 19:20:00 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[howto]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=309</guid>
		<description><![CDATA[Various reasons may cause SQL Server to get in a rut and not empty the transaction log of a database. In my case, our database backups were failing without our knowledge for several weeks, so the backups were never successful, and the transaction logs of a few databases grew so large that the backup process [...]]]></description>
			<content:encoded><![CDATA[<p>Various reasons may cause SQL Server to get in a rut and not empty the transaction log of a database. In my case, our database backups were failing without our knowledge for several weeks, so the backups were never successful, and the transaction logs of a few databases grew so large that the backup process would still not clear out the transaction log. In one case, we had a 187MB database with a <span style="text-decoration: underline;">37GB</span> transaction log!</p>
<p>The insanity had to stop! A handful of databases like this would put us over the top on that particular server's hard drive storage.</p>
<p>The SQL Server GUI for shrinking the database rendered no effect, and even using the DBCC SHRINKFILE command was not working.</p>
<p>The key, as explained by <a href="http://blog.sqlauthority.com/2006/12/30/sql-server-shrinking-truncate-log-file-log-full/" target="_blank">Pinal Dave</a>, is to run the SHRINKFILE command <em>twice, </em>with an explicit backup log truncation in between both runs. This code here will get you up and running:</p>
<div class="igBar"><span id="lsql-2"><a href="#" onclick="javascript:showCodeTxt('sql-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-2">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DBCC SHRINKFILE<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #ff0000;">"MyDatabase_Log"</span>, <span style="color: #cc66cc;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">BACKUP LOG MyDatabase <span style="color: #993333; font-weight: bold;">WITH</span> TRUNCATE_ONLY</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DBCC SHRINKFILE<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #ff0000;">"MyDatabase_Log"</span>, <span style="color: #cc66cc;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This freed up dozens of gigabytes on our server.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/shrink-the-unshrinkable-sql-transaction-log/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Using SQL to Retrieve SQL</title>
		<link>http://blog.crankybit.com/sql-to-retrieve-sql/</link>
		<comments>http://blog.crankybit.com/sql-to-retrieve-sql/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 12:03:39 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[howto]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=296</guid>
		<description><![CDATA[At work, someone made a request that required me to look through potentially hundreds of views in dozens of databases on our SQL Server. I certainly didn't want to examine each one at a time. How could I speed up this process with code? Well, you can find all of your views by querying the sysobjects [...]]]></description>
			<content:encoded><![CDATA[<p>At work, someone made a request that required me to look through potentially hundreds of views in dozens of databases on our SQL Server. I certainly didn't want to examine each one at a time. How could I speed up this process with code?</p>
<p>Well, you can find all of your views by querying the <em>sysobjects </em>table, and you can retrieve the SQL behind the views by querying the <em>syscomments </em>table. Something like this works well:</p>
<div class="igBar"><span id="lsql-7"><a href="#" onclick="javascript:showCodeTxt('sql-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-7">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> RTrim<span style="color:#006600; font-weight:bold;">&#40;</span>sysobjects.name<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> ViewName,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">       RTrim<span style="color:#006600; font-weight:bold;">&#40;</span>syscomments.text<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> ViewSQL</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">FROM</span>   sysobjects <span style="color: #993333; font-weight: bold;">JOIN</span> syscomments</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">ON</span>     syscomments.id=sysobjects.id</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">WHERE</span>  sysobjects.xtype=<span style="color: #ff0000;">'V'</span> <span style="color: #993333; font-weight: bold;">AND</span> sysobjects.category=<span style="color: #cc66cc;color:#800000;">0</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This will retrieve the SQL code and names of all the views in the current database. This simple query is the heart of the solution. But I would like to retrieve this information for <em>all </em>of the databases.</p>
<p>Well, it's easy enough to get a list of all the databases. The <em>sysdatabases</em> table in the <em>master</em> database has that list. You can query that table, perhaps filtering out some of the system or sample databases included with SQL Server:</p>
<div class="igBar"><span id="lsql-8"><a href="#" onclick="javascript:showCodeTxt('sql-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-8">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> name <span style="color: #993333; font-weight: bold;">FROM</span> master.dbo.sysdatabases</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #ff0000;">'tempdb'</span>,<span style="color: #ff0000;">'master'</span>,<span style="color: #ff0000;">'msdb'</span>,<span style="color: #ff0000;">'pubs'</span>,<span style="color: #ff0000;">'model'</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> name </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now just combine this information. To accomplish this, we'll build a stored procedure that will create a temporary table, loop through the databases and query each one for its views, insert the view information into the temporary table, and return the temporary table.</p>
<p>Something like this will do the trick:</p>
<div class="igBar"><span id="lsql-9"><a href="#" onclick="javascript:showCodeTxt('sql-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-9">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">CREATE</span> PROC dbo.selectViews   <span style="color: #993333; font-weight: bold;">AS</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">BEGIN</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">-- Vars</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DECLARE @dbname sysname</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">-- Temp Table</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #808080; font-style: italic;">#Results</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#40;</span>  </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  DatabaseName varchar<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">200</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  ViewName varchar<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">200</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  ViewText nvarchar<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">4000</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">-- Loop Thru the Databases.</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DECLARE dbnames_cursor CURSOR</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">FOR</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #993333; font-weight: bold;">SELECT</span> name <span style="color: #993333; font-weight: bold;">FROM</span> master.dbo.sysdatabases</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #ff0000;">'tempdb'</span>,<span style="color: #ff0000;">'master'</span>,<span style="color: #ff0000;">'msdb'</span>,<span style="color: #ff0000;">'pubs'</span>,<span style="color: #ff0000;">'model'</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> name</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">OPEN dbnames_cursor</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">FETCH NEXT <span style="color: #993333; font-weight: bold;">FROM</span> dbnames_cursor <span style="color: #993333; font-weight: bold;">INTO</span> @dbname</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">WHILE <span style="color:#006600; font-weight:bold;">&#40;</span>@@FETCH_STATUS &amp;lt;&amp;gt; -<span style="color: #cc66cc;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">BEGIN</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #993333; font-weight: bold;">IF</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@@FETCH_STATUS &amp;lt;&amp;gt; -<span style="color: #cc66cc;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  BEGIN   </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">-- Grab the Views of this Database and Put them in the Temp Table.</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #993333; font-weight: bold;">SET</span> @dbname = RTRIM<span style="color:#006600; font-weight:bold;">&#40;</span>@dbname<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #808080; font-style: italic;">#Results</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    EXECUTE</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #ff0000;">'SELECT '</span><span style="color: #ff0000;">''</span>+@dbName+<span style="color: #ff0000;">''</span><span style="color: #ff0000;">' as DatabaseName, '</span> +</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #ff0000;">'RTrim('</span> + @dbname + <span style="color: #ff0000;">'.dbo.sysobjects.name) as ViewName, '</span> +</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #ff0000;">'RTrim('</span> + @dbname + <span style="color: #ff0000;">'.dbo.syscomments.text) as ViewText '</span> +</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #ff0000;">'FROM '</span> + @dbName + <span style="color: #ff0000;">'.dbo.sysobjects join '</span> + @dbName + <span style="color: #ff0000;">'.dbo.syscomments '</span> +</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #ff0000;">'ON '</span> + @dbName + <span style="color: #ff0000;">'.dbo.syscomments.id='</span> + @dbName + <span style="color: #ff0000;">'.dbo.sysobjects.id '</span> +</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #ff0000;">'WHERE '</span> + @dbname + <span style="color: #ff0000;">'.dbo.sysobjects.xtype='</span><span style="color: #ff0000;">'V'</span><span style="color: #ff0000;">' and '</span> + @dbname + <span style="color: #ff0000;">'.dbo.sysobjects.category=0 '</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  END</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  FETCH NEXT <span style="color: #993333; font-weight: bold;">FROM</span> dbnames_cursor <span style="color: #993333; font-weight: bold;">INTO</span> @dbname</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">END</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CLOSE dbnames_cursor</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DEALLOCATE dbnames_cursor</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #808080; font-style: italic;">#Results order by DatabaseName, ViewName</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #808080; font-style: italic;">#Results</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">END</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">-- </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now just execute the stored procedure and review its output.</p>
<div class="igBar"><span id="lsql-10"><a href="#" onclick="javascript:showCodeTxt('sql-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-10">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">exec selectViews </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This portion of the solution just retrieves the data. After writing this, I developed a really short and simple ColdFusion application that would output the database name, view name, and SQL to a table, and used some simple JavaScript to make it easier to search and filter the views. The client-side methods used to view and work with the data are obviously up to you.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/sql-to-retrieve-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Backward Use of CASE in SQL</title>
		<link>http://blog.crankybit.com/backward-use-of-case-in-sql/</link>
		<comments>http://blog.crankybit.com/backward-use-of-case-in-sql/#comments</comments>
		<pubDate>Fri, 09 Nov 2007 11:15:57 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[General Web Dev.]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/backward-use-of-case-in-sql/</guid>
		<description><![CDATA[I was updating an old app to use SQL on a new SQL Server database rather than an older Access database. As any database programmer is aware, there are irritating inconsistencies between Access SQL and SQL Server's T-SQL, such as absence of the IIF() and Trim() functions in T-SQL. So I was going through the [...]]]></description>
			<content:encoded><![CDATA[<p>I was updating an old app to use SQL on a new SQL Server database rather than an older Access database. As any database programmer is aware, there are irritating inconsistencies between Access SQL and SQL Server's T-SQL, such as absence of the IIF() and Trim() functions in T-SQL.</p>
<p>So I was going through the SQL and making these changes. I came across a very ugly IIF() function similar to this:</p>
<div class="igBar"><span id="lsql-14"><a href="#" onclick="javascript:showCodeTxt('sql-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-14">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">IIF<span style="color:#006600; font-weight:bold;">&#40;</span>phLead=True,<span style="color: #ff0000;">'LEAD'</span>, </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">IIF<span style="color:#006600; font-weight:bold;">&#40;</span>phFax=True,<span style="color: #ff0000;">'FAX'</span>, </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">IIF<span style="color:#006600; font-weight:bold;">&#40;</span>phVVM=True,<span style="color: #ff0000;">'VVM'</span>, <span style="color: #ff0000;">'DEPT'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Take that statement and add even more boolean fields. Clearly, the intent was to have a calculated field that had a friendly name based on which boolean field was true.</p>
<p>So, how can this be easily converted into a CASE statement? As it stands, CASE statements are actually more verbose than the IIF() function, and the IIF() function above is already hard to read and verbose as it is.</p>
<p>A direct code conversion would be something like this:</p>
<div class="igBar"><span id="lsql-15"><a href="#" onclick="javascript:showCodeTxt('sql-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-15">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CASE phLead WHEN <span style="color: #cc66cc;color:#800000;">1</span> THEN <span style="color: #ff0000;">'LEAD'</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ELSE CASE phFax WHEN <span style="color: #cc66cc;color:#800000;">1</span> THEN <span style="color: #ff0000;">'FAX'</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ELSE CASE phVVM WHEN <span style="color: #cc66cc;color:#800000;">1</span> THEN <span style="color: #ff0000;">'VVM'</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ELSE <span style="color: #ff0000;">'DEPT'</span> END END END </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Ugly, ugly, ugly. The cool thing about CASE that differs from IIF() is that you can attack it from the other direction. Instead of saying, "Is phLead true? Is phFax true? Is phVVM true?", you can instead say, "Okay, who's true? phLead? phFax? phVVM?" It sounds negligible, but the code is shorter and easier to read:</p>
<div class="igBar"><span id="lsql-16"><a href="#" onclick="javascript:showCodeTxt('sql-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-16">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CASE <span style="color: #cc66cc;color:#800000;">1</span> WHEN phLead THEN <span style="color: #ff0000;">'LEAD'</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">WHEN phFax THEN <span style="color: #ff0000;">'FAX'</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">WHEN phVVM then <span style="color: #ff0000;">'VVM'</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ELSE <span style="color: #ff0000;">'DEPT'</span> END </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Ahh, much better.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/backward-use-of-case-in-sql/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Backup and Restore Your SQL Database</title>
		<link>http://blog.crankybit.com/backup-and-restore-your-sql-database/</link>
		<comments>http://blog.crankybit.com/backup-and-restore-your-sql-database/#comments</comments>
		<pubDate>Thu, 16 Nov 2006 18:54:00 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/backup-and-restore-your-sql-database/</guid>
		<description><![CDATA[It's easy to backup or restore your database directly with SQL. backup database MyDatabase TO DISK='c:pathtoMyDatabase.bak' with INIT The syntax is pretty self-explanatory once you see it. Backup the database "TO DISK", or to a file at the designated path. The "WITH INIT" specifies that any existing backup file will be overwritten. The restore syntax [...]]]></description>
			<content:encoded><![CDATA[<p>It's easy to backup or restore your database directly with SQL.</p>
<blockquote><p>backup database <em>MyDatabase</em> TO DISK=<em>'c:pathtoMyDatabase.bak'</em> with INIT</p></blockquote>
<p>The syntax is pretty self-explanatory once you see it. Backup the database "TO DISK", or to a file at the designated path. The "WITH INIT" specifies that any existing backup file will be overwritten.</p>
<p>The restore syntax is nearly identical. If the database you are restoring currently exists, be sure to <em>drop the database</em> before doing the restore.</p>
<blockquote><p>restore database <em>MyDatabase</em> FROM DISK=<em>'c:pathtoMyDatabase.bak'</em></p></blockquote>
<p>Simple as that.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/backup-and-restore-your-sql-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

