<?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>James Gregory &#187; SQL</title>
	<atom:link href="http://jagregory.com/writings/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://jagregory.com</link>
	<description>Monkeying with the code</description>
	<lastBuildDate>Tue, 22 Jun 2010 11:07:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SQL Server Object Exists Function</title>
		<link>http://jagregory.com/writings/sql-server-object-exists-function/</link>
		<comments>http://jagregory.com/writings/sql-server-object-exists-function/#comments</comments>
		<pubDate>Fri, 26 May 2006 12:33:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=11</guid>
		<description><![CDATA[Update: Added separate versions for SQL Server 2000 and SQL Server 2005, due to the differences in the system objects tables.It may just be me, but when writing migration/create scripts for use with SQL Server I get quite agitated at having to write an ugly, long-winded, drop statement at the start of every object definition.
The [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> Added separate versions for SQL Server 2000 and SQL Server 2005, due to the differences in the system objects tables.It may just be me, but when writing migration/create scripts for use with SQL Server I get quite agitated at having to write an ugly, long-winded, drop statement at the start of every object definition.</p>
<p>The support for dropping objects is one of the few things I would say MySQL has SQL Server over the barrel for.Baring in mind that if you try to drop an object that doesn’t exist, you’ll get an execution error; here’s how to drop a table in MySQL:</p>
<pre><code>DROP TABLE IF EXISTS customers</code></pre>
<p>Here’s how to drop the same table, if you’re using SQL Server:</p>
<pre><code>IF EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'customers') AND type = (N'U'))    DROP TABLE customers</code></pre>
<p>As always, when something annoys you enough and you’re in the middle of something else, it’s about time you wrote that solution. So I’ve created a simple user-defined function that checks if an object exists then returns a <code>BIT 0</code> or <code>1</code> depending.</p>
<p>To use the function, all you have to do is call <code>dbo.ObjectExists</code> with two parameters, the first being the name of the object you want to check on, the second being the type of object.</p>
<pre><code>IF dbo.ObjectExists('customers', 'U') = 1</code></pre>
<pre><code>    DROP TABLE customers</code></pre>
<dl>
<dt>Common Object Types:</dt>
<dd><code>P</code> &#8211; Stored Procedure</dd>
<dd><code>U</code> &#8211; User Table</dd>
<dd><code>FN</code> &#8211; User-Defined Function</dd>
</dl>
<p>Thanks to this little function, you can now almost match the simplicity of MySQL.</p>
<h3>…and now the code</h3>
<h4>SQL Server 2000</h4>
<pre><code>CREATE FUNCTION dbo.ObjectExists(@Object VARCHAR(100), @Type VARCHAR(2)) RETURNS BIT</code></pre>
<pre><code>AS</code></pre>
<pre><code>BEGIN</code></pre>
<pre><code>    DECLARE @Exists BIT</code></pre>
<pre><code>    IF EXISTS(SELECT 1 FROM dbo.sysobjects WHERE [ID] = OBJECT_ID(@Object) AND type = (@Type))</code></pre>
<pre><code>        SET @Exists = 1</code></pre>
<pre><code>    ELSE</code></pre>
<pre><code>        SET @Exists = 0</code></pre>
<pre><code>    RETURN @Exists</code></pre>
<pre><code>END</code></pre>
<h4>SQL Server 2005</h4>
<pre><code>CREATE FUNCTION dbo.ObjectExists(@Object VARCHAR(100), @Type VARCHAR(2)) RETURNS BIT</code></pre>
<pre><code>AS</code></pre>
<pre><code>BEGIN</code></pre>
<pre><code>    DECLARE @Exists BIT</code></pre>
<pre><code>    IF EXISTS(SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(@Object) AND type = (@Type)) </code></pre>
<pre><code>       SET @Exists = 1</code></pre>
<pre><code>    ELSE</code></pre>
<pre><code>        SET @Exists = 0</code></pre>
<pre><code>    RETURN @Exists</code></pre>
<pre><code>END</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/sql-server-object-exists-function/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>SQL Server: Exists() function quicker than Count()</title>
		<link>http://jagregory.com/writings/sql-server-exists-function-quicker-than-count/</link>
		<comments>http://jagregory.com/writings/sql-server-exists-function-quicker-than-count/#comments</comments>
		<pubDate>Thu, 22 Sep 2005 23:12:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=35</guid>
		<description><![CDATA[You’ve seen the title; it’s true.
I’ve been running an “administration” query to modify a table with around 190,000 records in. This table, segments, contains a status column called Coded. This value is derived from another table, faults. If any faults belonging to a segment has a coded status of 0 then the segment coded value [...]]]></description>
			<content:encoded><![CDATA[<p>You’ve seen the title; it’s true.</p>
<p>I’ve been running an “administration” query to modify a table with around 190,000 records in. This table, segments, contains a status column called Coded. This value is derived from another table, faults. If any faults belonging to a segment has a coded status of 0 then the segment coded value is also 0.</p>
<p>My query did an update on each row in the segments table, retrieving a Count of how many faults there are with a coded status of 0 that belong to the current segment. If the Count is greater than 0 then the segment coded is set to 0, otherwise to 1.</p>
<p>It ran for over 2 hours without any sign of finishing.</p>
<p>I changed the query to do a simple case statement with an Exists on the faults table where Coded = 0 and it’s just finished in 4 minutes. Unbelivable.</p>
<h3>Update</h3>
<p>I thought I should give a bit of an explanation as to why this is happening. It basically all boils down to the way the two functions operate that makes the biggest difference. A Count statement counts every row that matches the where statement, so even if you only need to know about one record (like in my case) it’ll still read every other row too. On the other hand the Exists function will return as soon as its conditions are met, so if the first row it finds matches it’ll only read one row.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/sql-server-exists-function-quicker-than-count/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging Glory</title>
		<link>http://jagregory.com/writings/debugging-glory/</link>
		<comments>http://jagregory.com/writings/debugging-glory/#comments</comments>
		<pubDate>Tue, 03 May 2005 22:52:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=44</guid>
		<description><![CDATA[Debugging is a great thing… Often though I’ve drempt of some greater, more useful debugger. It would have helped if I’d actually gone out and looked for such a thing.
Anyway, I haven’t found a better debugger. I’ve found how to use the current one better! I’ve enabled client-side script debugging, which is fantastic. No more [...]]]></description>
			<content:encoded><![CDATA[<p>Debugging is a great thing… Often though I’ve drempt of some greater, more useful debugger. It would have helped if I’d actually gone out and looked for such a thing.</p>
<p>Anyway, I haven’t found a better debugger. I’ve found how to use the current one better! I’ve enabled client-side script debugging, which is fantastic. No more using IE or Firefoxes vague error messages, I can now step through code just as you would VB/C#.Net. The big one for me though has to be the ability to debug Stored Procedures, yes SQL Server Stored Procedures. Breakpoints, step-through, the lot.</p>
<p>Client-side debugging is so obvious I was kicking my self when I got it working. All you need to do is go into Tools &gt; Internet Options &gt; Advanced and check the “Enable Client-Side Debugging” in Internet explorer. You may need to do some jiggery-pokery in VS but I can’t remeber what.</p>
<p>SqlServer is a bit more complicated, you need to copy the mssdi98.dll from your [visual studio installation]\sqlserver folder into your sqlserver instances binn folder. Then open up Enterprise Manager and grant Exec permissions to the user you use to log-in (or the Builtin\Administrators group) for the extended procedure sp_sdidebug. Lastly in your project options in VS mark the Enable Sql Debugging. That should be it, from the server explorer in VS you should now be able to choose “step-into” from any stored procedure, function or trigger!</p>
<p>Productivity++</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/debugging-glory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
