<?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 Wiseman &#187; SQL Server</title>
	<atom:link href="http://www.jameswiseman.com/blog/tag/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jameswiseman.com/blog</link>
	<description>My Programming Notebook and Other Things</description>
	<lastBuildDate>Sun, 05 Feb 2012 14:18:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Counting Character Instances in a SQL String</title>
		<link>http://www.jameswiseman.com/blog/2011/05/16/counting-character-instances-in-a-sql-string/</link>
		<comments>http://www.jameswiseman.com/blog/2011/05/16/counting-character-instances-in-a-sql-string/#comments</comments>
		<pubDate>Mon, 16 May 2011 13:01:45 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL-Strings]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2975</guid>
		<description><![CDATA[I discovered this little trick today. If you&#8217;re wanting to count the number of characters present in a given SQL Server varchar variable, or common, use the following convention --assuming a search character contained in the variable @SeekChar --from a variable SELECT LEN(@StringVariableName) - LEN(REPLACE(@StringVariableName,@SeekChar,'')) --from a table field SELECT LEN(SomeTableCol) - LEN(REPLACE(SomeTableCol, @SeekChar, '')) [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I discovered this little trick today.</p>
<p>If you&#8217;re wanting to count the number of characters present in a given SQL Server varchar variable, or common, use the following convention</p>
<pre class="SQL" name="code">--assuming a search character contained in the variable @SeekChar

--from a variable
SELECT LEN(@StringVariableName) - LEN(REPLACE(@StringVariableName,@SeekChar,''))

--from a table field
SELECT LEN(SomeTableCol) - LEN(REPLACE(SomeTableCol, @SeekChar, ''))
FROM SomeTable

--The Oracle  PL/SQL Variant (for the hell of it).
--All we have to do is replace the LEN function with LENGTH!
SELECT LENTH(SomeTableCol)-LENGTH(REPLACE(SomeTableCol, @SeekChar, ''))
FROM SomeTable</pre>
<p>Quick and easy and no annoying looping constructs.</p>
<div class="shr-publisher-2975"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F05%2F16%2Fcounting-character-instances-in-a-sql-string%2F' data-shr_title='Counting+Character+Instances+in+a+SQL+String+'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F05%2F16%2Fcounting-character-instances-in-a-sql-string%2F' data-shr_title='Counting+Character+Instances+in+a+SQL+String+'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/05/16/counting-character-instances-in-a-sql-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting Default SQL Server Database Using ISQL</title>
		<link>http://www.jameswiseman.com/blog/2011/05/10/setting-default-sql-server-database-using-isql/</link>
		<comments>http://www.jameswiseman.com/blog/2011/05/10/setting-default-sql-server-database-using-isql/#comments</comments>
		<pubDate>Tue, 10 May 2011 18:55:13 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Database Connection]]></category>
		<category><![CDATA[ISQL]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2946</guid>
		<description><![CDATA[You might be familiar with the following scenario: You have a SQL Server database server hosting more than one database, but you just so happen to be accessing one in particular from your development environment (Query Analyser, Management Studio, etc). So, you run a little query to automatically connect you to this database when you [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><div class="zemanta-img zemanta-action-dragged" style="margin: 1em; display: block;">
<div class="wp-caption alignright" style="width: 250px"><a href="http://www.flickr.com/photos/36724437@N03/4297955162"><img title="Problems????" src="http://www.jameswiseman.com/blog/wp-content/uploads/2011/05/4297955162_3dcbd7344c_m1.jpg" alt="Problems????" width="240" height="167" /></a><p class="wp-caption-text">Image by essecento via Flickr</p></div>
</div>
<p>You might be familiar with the following scenario:</p>
<p>You have a SQL Server database server hosting more than one database, but you just so happen to be accessing one in particular from your development environment (Query Analyser, Management Studio, etc).</p>
<p>So, you run a little query to automatically connect you to this database when you connect to the server. You might try something like:</p>
<pre name="code" class="SQL">Exec sp_defaultdb @loginame='DOMAIN\UsrNam', @defdb='SomeDatabase';</pre>
<p>So, now when you log on, your database is ready and waiting to be used without having to manually select it. I have found this scenario common in development environments, when you are typically running lots of ad-hoc SQL queries, or running in code (stored procedures, views, triggers, etc) against the database.</p>
<p>However if you are working on different databases on the same server, defaulting to a particular one can cause you problems if you need to run in code against a different database. On occasions I have applied stored procedures to the default database, when I meant to do it against another one on the server!</p>
<p>Another consequence is when a development database is refreshed or rebuilt from some external source (from a live environment, for example). Although your default database may exist by name, this is not enough for SQL Server, which fails to log you on and gives the error message:</p>
<blockquote><p><em>Cannot open user default database.</em></p></blockquote>
<p>You will also get this if you have chosen to rename the database.</p>
<p>So what to do in this instance?</p>
<p>Well, we can defer to the trusty <a title="isql Utility" href="http://msdn.microsoft.com/en-us/library/aa214007(v=sql.80).aspx" target="_blank">ISQL command line utility</a>, as shown in the following screenshot:</p>
<p><a href="http://www.jameswiseman.com/blog/wp-content/uploads/2011/05/ISQL_CommandLine_SetDefaultDB.png"><img class="size-full wp-image-2949 alignnone" title="ISQL_CommandLine_SetDefaultDB" src="http://www.jameswiseman.com/blog/wp-content/uploads/2011/05/ISQL_CommandLine_SetDefaultDB.png" alt="" width="669" height="530" /></a></p>
<p>Simply follow these steps:</p>
<ol>
<li>Open a DOS  command Prompt</li>
<li>Open up an <strong>ISQL</strong> Session. I used the following command:
<ul>
<li>-S SQLServer_Name &#8211; Specify the name of the SQL Server to which you are connecting.</li>
<li>-E &#8211; Stipulate to connect as a trusted connection (Windows Authentication)</li>
<li>-d master &#8211; The database name to which you are going to connect. Here I specified &#8216;master&#8217;</li>
</ul>
</li>
<p>isql -S SQLServer_Name -E -d master</p>
<p>The three options user above are as follows:</p>
<p>Of course, you can connect however you want. For more options (also shown in the screenshot above), type in<strong> isql /?</strong> for a full list, or refer to the site on the above link.</p>
<li>Now run a version of the above script, setting the default database to master</li>
<pre name="code" class="SQL">Exec sp_defaultdb @loginame='DOMAIN\UsrNam', @defdb='master';</pre>
<li>To apply this, enter &#8216;GO&#8217; on the next line.</li>
</ol>
<p>This will ensure your SQL Server defaults you to the &#8216;master&#8217; database in all subsequent connections (unless you change them again). I would counsel this as general good practice, and it comes recommended by a friendly DBA I happen to know.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/pixy.gif?x-id=2ce7669a-79d0-41e5-8855-def20914561c" alt="" /></div>
<div class="shr-publisher-2946"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F05%2F10%2Fsetting-default-sql-server-database-using-isql%2F' data-shr_title='Setting+Default+SQL+Server+Database+Using+ISQL'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F05%2F10%2Fsetting-default-sql-server-database-using-isql%2F' data-shr_title='Setting+Default+SQL+Server+Database+Using+ISQL'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/05/10/setting-default-sql-server-database-using-isql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Single Result SQL Subquery Assignment</title>
		<link>http://www.jameswiseman.com/blog/2010/11/16/single-result-sql-subquery-assignment/</link>
		<comments>http://www.jameswiseman.com/blog/2010/11/16/single-result-sql-subquery-assignment/#comments</comments>
		<pubDate>Tue, 16 Nov 2010 08:00:32 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Constraint]]></category>
		<category><![CDATA[ISNULL]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>
		<category><![CDATA[Subquery]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=1753</guid>
		<description><![CDATA[Does that title make any sense to you? If not, can you bear with me? I&#8217;ll make it worth your while, I promise! I&#8217;ve been looking at a bit of code that threw an error for the first time in nearly a year. This &#8216;bit&#8217; of code is present on four different environments, and is [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Does that title make any sense to you?</p>
<p>If not, can you bear with me? I&#8217;ll make it worth your while, I promise!</p>
<p>I&#8217;ve been looking at a bit of code that threw an error for the first time in nearly a year. This &#8216;bit&#8217; of code is present on four different environments, and is run under some particularly strenuous and varied conditions.</p>
<p>Here is the gist of the code:</p>
<pre name="code" class="sql">SELECT @var = ISNULL((SELECT col1 FROM SomeTable WHERE col2='SomeValue'), 0)</pre>
<p>Those gurus (and some lesser mortals) among us will spot the potential problem immediately. For those, this sort of issue sticks out like sore thumb. But, for the rest of us it isn&#8217;t so inherently obvious.</p>
<p>Let me demonstrate. Imagine our &#8216;SomeTable&#8217; has the following data:</p>
<table>
<tbody>
<tr>
<td><strong>Col1</strong></td>
<td><strong>Col2</strong></td>
</tr>
<tr>
<td>1</td>
<td>SomeValue</td>
</tr>
<tr>
<td>2</td>
<td>SomeValue</td>
</tr>
</tbody>
</table>
<p>We are assuming that statement we are passing to our <strong>ISNULL</strong> function will only ever return one result. However, in the above example it does not. You can try it yourself; simply paste the SQL below into your query window:</p>
<pre name="code" class="sql">create table #temp (col1 int, col2 varchar(10));
insert into #temp values (1,'SomeValue');
insert into #temp values (2,'SomeValue');
select isnull((select col1 from #temp where col2 = 'SomeValue'),0)
drop table #temp</pre>
<p>You should get the following message:</p>
<pre style="color: red;">Msg 512, Level 16, State 1, Line 4
Subquery returned more than 1 value. This is not permitted when the subquery
follows =, !=, &lt;, &lt;= , &gt;, &gt;= or when the subquery is used as an expression.</pre>
<p>Understanding the data is key to resolving this. You have assumed there will only be one value, so what are you prepared to accept? Possible solutions include:</p>
<pre name="code" class="sql">select isnull((select max(col1) from #temp where col2 = 'SomeValue'),0)
select isnull((select min(col1) from #temp where col2 = 'SomeValue'),0)
select isnull((select top 1 col1 from #temp where col2 = 'SomeValue'),0)
select isnull((select distinct col1 from #temp where col2 = 'SomeValue'),0)</pre>
<p>The first three solve the problem for you in all cases. In the fourth example, the use of the <strong>distinct</strong> clause will only work if your result set is identical. In fact, it will still break for our example.</p>
<p>But, by putting one of these fixes in place, we are maybe missing the whole point. Although defensive programming can help trap this sort of error, we are overlooking the fact that we have maybe stumbled upon some sort of data integrity error. I.e. do we have two records where we really shouldn&#8217;t?</p>
<p>Then the fix is something more fundamental, for example, are we missing a unique constraint somewhere? Here is our table definition with the constraint:</p>
<pre name="code" class="sql">create table #temp (col1 int, col2 varchar(10), constraint UK_COL2 unique (COL2));</pre>
<p>Running our original code block with this in place shows up the error when we try to insert a duplicate value in <strong>Col2</strong>:</p>
<pre style="color: red;">Msg 2627, Level 14, State 1, Line 3
Violation of UNIQUE KEY constraint 'UK_COL2'.
Cannot insert duplicate key in object 'dbo.#temp'.</pre>
<p>Sure, we still get the error, however, we get it earlier in our process, which is generally better. The is because we may not query these data for some time after it has been inserted, so it may be some time before the original error presented itself. In addition to this, the chances are that if you&#8217;ve coded a unique constraint then you&#8217;ll be more inclined to test for it.</p>
<p>So, a few things to think about. Principally, what do you want your subquery to return, and is your data correct?</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/pixy.gif?x-id=a28b9f4a-0b2d-4059-9705-d6de89412288" alt="" /><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<div class="shr-publisher-1753"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F11%2F16%2Fsingle-result-sql-subquery-assignment%2F' data-shr_title='Single+Result+SQL+Subquery+Assignment'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F11%2F16%2Fsingle-result-sql-subquery-assignment%2F' data-shr_title='Single+Result+SQL+Subquery+Assignment'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/11/16/single-result-sql-subquery-assignment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When Oracle OLEDB Provider Hangs Visual Studio</title>
		<link>http://www.jameswiseman.com/blog/2010/11/05/when-oracle-oledb-provider-hangs-visual-studio/</link>
		<comments>http://www.jameswiseman.com/blog/2010/11/05/when-oracle-oledb-provider-hangs-visual-studio/#comments</comments>
		<pubDate>Fri, 05 Nov 2010 14:11:34 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Data Dictionary]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SSRS]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=1723</guid>
		<description><![CDATA[I&#8217;ve been playing around with SSRS in SQL Server 2005 recently, and have been particularly encouraged by its potential as a central reporting tool. As part of this  I have been demonstrating how we can SSRS to produce reports from numerous different data sources and providers. In this particular instance I have been testing its [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I&#8217;ve been playing around with SSRS in SQL Server 2005 recently, and have been particularly encouraged by its potential as a central reporting tool. As part of this  I have been demonstrating how we can SSRS to produce reports from numerous different data sources and providers.</p>
<p>In this particular instance I have been testing its ability to produce reports from the output of an Oracle Package/Procedure and a SQL Server Stored Procedure.  Using the built-in .NET Client I was able to do this quite successfully with little fuss.</p>
<p>It was then brought to my attention that <a title="Microsoft deprecating Oracle Client from ADO.NET 4" href="http://blogs.msdn.com/b/adonet/archive/2009/06/15/system-data-oracleclient-update.aspx" target="_blank">Microsoft are deprecating Oracle Client from ADO.NET 4</a>. This means that, while the code may continue working for some considerable time, going forward it is a better strategy to use an alternative. So, I switched to using <a title="Oracle 11g Release 2 ODAC 11.2.0.1.2" href="http://www.oracle.com/technetwork/database/windows/downloads/index-101290.html" target="_blank">Oracle 11g Release 2 ODAC 11.2.0.1.2</a>.</p>
<p>It&#8217;s worth noting that for those wanting to utilise an Oracle Stored procedure in SSRS, it must return its data in an OUT REF CURSOR. There are also a few other conditions which you must abide by.  For further info see: <a title="Tap into Oracle Databases with SQL Server Reporting Services" href="http://www.developer.com/db/article.php/3524781">Tap into Oracle Databases with SQL Server Reporting Services</a>.</p>
<p>Anyway, on to the problem&#8230;</p>
<p>So, as already intimated, all was fine and dandy, until I switched provider. I had set up the dataset on my report, entered the Stored Procedure name, and clicked &#8216;Ok&#8217;. Nothing happened; the IDE just hung.</p>
<p>At this point, I should probably reproduce the procedure in question:</p>
<pre name="code" class="sql">PROCEDURE pck_mypackage.pr_myproc(L_CURSOR out SYS_REFCURSOR)
is
   BEGIN
     OPEN L_CURSOR FOR
       SELECT * FROM MyTable;
   END;</pre>
<p>And here the connection string (missing the credentials).</p>
<pre name="code" class="sql">Provider=OraOLEDB.Oracle.1;Data Source=DBSOURCE</pre>
<p>Nothing shocking whatsoever, right?</p>
<p>For the first day or so, I took to killing Visual Studio, tweaking a few settings and then trying again. No-one in the websphere seemed to have encountered this; I was baffled. I even resorted to <a title="Windows Process Monitor" href="http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx" target="_blank">Window&#8217;s Process Monitor</a> and trawling through the 1 million+ events it had logged.</p>
<p>So, I decided to confide in my old friend, the bullet-point list. I regaled it with the details of what was working, and what was not. Here it is, replete with the pertinent information:</p>
<p><strong>What Works?</strong></p>
<ul>
<li>The stored procedure executes correctly in PL/SQL Developer</li>
<li>The &#8216;test connect&#8217; in works fine in SSRS</li>
<li>A query string of SELECT * FROM MyTable; with Command Type of &#8216;text&#8217; produces the correct fields in the SSRS report.</li>
<li>.NET Oracle Provider instead of Oracle OLE DB Provider</li>
</ul>
<p><strong> What Fails:</strong></p>
<ul>
<li>If i change the Command Type to &#8216;Stored Procedure&#8217; and enter &#8216;pr_myproc&#8217;, when I click &#8216;OK&#8217; Visual Studio 2005 (service pack 2) simply hangs/crashes.</li>
</ul>
<p>My attention was drawn to the last three words, &#8220;simply hangs/crashes.&#8221; It was then I noticed that Visual Studio was not actually hanging completely. I could still click the top-right window buttons; it just seemed to be waiting.</p>
<p>I confided in a friendly DBA, who suggested I looked at the Oracle sessions in an attempt to determine:</p>
<ol>
<li>Whether or not a connection was actually being made, and,</li>
<li>What it was doing if the connection was successful</li>
</ol>
<p>Something must have piqued his interest, because he decided to do it there for me himself.</p>
<p>&#8220;Ahhh,&#8221; he said, &#8220;you&#8217;re querying sys.obj$.&#8221; He then proceeded to explain to me why exactly this was an issue. The schema we were searching had substantially large data dictionary. This dictionary has to be queried in order to bring back the procedure parameters and the REFCURSOR output.  When looking up the information, the two providers (.NET and Oracle) used two different queries that had a dramatic relative difference in their performances.</p>
<p>Here is the one the Oracle Provider used, taking 10+ minutes:</p>
<pre name="code" class="sql">select * from (select null  PROCEDURE_CATALOG
                      , owner PROCEDURE_SCHEMA
                      , object_name PROCEDURE_NAME
                      , decode (object_type, 'PROCEDURE', 2, 'FUNCTION',  3, 1) PROCEDURE_TYPE
                      , null PROCEDURE_DEFINITION
                      , null DESCRIPTION
                      , created DATE_CREATED
                      , last_ddl_time DATE_MODIFIED
              from all_objects where object_type in ('PROCEDURE','FUNCTION')
              union all
              select null PROCEDURE_CATALOG
                          , arg.owner PROCEDURE_SCHEMA
                          , arg.package_name||'.'||arg.object_name PROCEDURE_NAME
                          , decode(min(arg.position), 0, 3, 2) PROCEDURE_TYPE
                          , null PROCEDURE_DEFINITION
                          , decode(arg.overload, '', '', 'OVERLOAD') DESCRIPTION
                          , min(obj.created) DATE_CREATED
                          , max(obj.last_ddl_time) DATE_MODIFIED
              from all_objects obj, all_arguments arg
              where arg.package_name is not null
              and   arg.owner = obj.owner
              and   arg.object_id = obj.object_id
              group by arg.owner, arg.package_name, arg.object_name, arg.overload ) PROCEDURES
WHERE PROCEDURE_NAME = 'pck_my_package.pr_myproc' order by 2, 3</pre>
<p>The second SQL Statement in the union is a particular killer. Here, it joins <strong>all_objects</strong> and <strong>all_arguments </strong>tables! I actually ran this query in PL/SQL developer, but gave up after it failed to finish in thirty minutes.</p>
<p>This a particular problem of those schemas with large data dictionaries. I tried this in a smaller one, further up our shipping chain, and it returned in an acceptable amount of time.</p>
<div class="shr-publisher-1723"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F11%2F05%2Fwhen-oracle-oledb-provider-hangs-visual-studio%2F' data-shr_title='When+Oracle+OLEDB+Provider+Hangs+Visual+Studio'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F11%2F05%2Fwhen-oracle-oledb-provider-hangs-visual-studio%2F' data-shr_title='When+Oracle+OLEDB+Provider+Hangs+Visual+Studio'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/11/05/when-oracle-oledb-provider-hangs-visual-studio/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Worrying About SQL Dependencies</title>
		<link>http://www.jameswiseman.com/blog/2010/09/17/sql-sysdepends/</link>
		<comments>http://www.jameswiseman.com/blog/2010/09/17/sql-sysdepends/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 21:24:13 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Dependencies]]></category>
		<category><![CDATA[Impact Analsys]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=1452</guid>
		<description><![CDATA[In an automatic shipping/deployment system you may find yourself having to determine object/item dependencies, and stipulate a shipping order manually. Image by Wonderlane via Flickr For example, when shipping SQL Server code, you might consider the following order of shipping precedence for database scripts: Structural creation Structural changes Relationships Static Data Views Stored Procedures Triggers [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>In an automatic shipping/deployment system you may find yourself having to determine object/item dependencies, and stipulate a shipping order manually.</p>
<div class="zemanta-img zemanta-action-dragged" style="margin: 1em; display: block;">
<div>
<dl class="wp-caption alignright" style="width: 250px;">
<dt class="wp-caption-dt"><a href="http://www.flickr.com/photos/71401718@N00/2062184804"><img title="Controlling IT Costs; Enterprise Architecture ..." src="http://www.jameswiseman.com/blog/wp-content/uploads/2010/09/2062184804_744aa9be6d_m.jpg" alt="Controlling IT Costs; Enterprise Architecture ..." width="240" height="160" /></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image by <a href="http://www.flickr.com/photos/71401718@N00/2062184804">Wonderlane</a> via Flickr</dd>
</dl>
</div>
</div>
<p>For example, when shipping SQL Server code, you might consider the following order of shipping precedence for database scripts:</p>
<ol>
<li>Structural creation</li>
<li>Structural changes</li>
<li>Relationships</li>
<li>Static Data</li>
<li>Views</li>
<li>Stored Procedures</li>
<li>Triggers</li>
</ol>
<p>Views that depend on other views have to be shipped in the appropriate order, so you will need to consider this as a factor.</p>
<p>In the background we have a dependency table (<span style="color: #008000;"><strong>sysdepends</strong></span> for SQL 2000 and <span style="color: #008000;"><strong>sys.sql_dependencies</strong></span> for 2005/2008).</p>
<p>For the purposes of this article, I am interested in the creation and omission of records from this table for <strong>Stored Procedure</strong> dependencies.</p>
<p>So, let us create two simple stored procedures, <strong>root </strong>and <strong>dependent</strong> like so:</p>
<pre class="sql" name="code">create procedure root as
select 1
go

create procedure dependent as
exec root
go</pre>
<p>So problems anywhere, and the execution of <strong>dependent</strong> works a treat:</p>
<pre class="sql" name="code">exec dependent;</pre>
<p>Moreover, when I look at the <strong><span style="color: #008000;">sys.sql_dependencies </span><span style="color: #008000;"><span style="font-weight: normal;">table, I see an entry:</span></span></strong></p>
<pre class="sql" name="code">select sd.*
from sys.sql_dependencies sd
inner join sys.objects so on so.object_id = sd.object_id
where so.name = 'dependent'</pre>
<p>However, when I add these in reverse (I.e. add the dependent Stored Procedure first), I get the following message:</p>
<blockquote><p>Cannot add rows to sysdepends for the current stored procedure because it depends on the missing object &#8216;dependent&#8217;</p></blockquote>
<p>With both stored procedures compiled, the code still works as before, however no entry is added retrospectively to the SQL table when &#8216;root&#8217; is compiled after &#8216;dependent&#8217;</p>
<p>Although this has no practical impact with regard to execution, your ability to perform effective impact analysis for testing and refactoring purposes.</p>
<p>Fortunately, on SQL2005 and 2008, this can be remedied by executing the <strong>sp_refreshsqlmodule</strong> stored procedure.</p>
<p>And there we have it!</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/pixy.gif?x-id=ffa9105d-e082-460a-b590-18fcd5c09ea6" alt="" /><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<div class="shr-publisher-1452"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F09%2F17%2Fsql-sysdepends%2F' data-shr_title='Worrying+About+SQL+Dependencies'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F09%2F17%2Fsql-sysdepends%2F' data-shr_title='Worrying+About+SQL+Dependencies'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/09/17/sql-sysdepends/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server Database Version Database</title>
		<link>http://www.jameswiseman.com/blog/2010/09/08/sql-server-database-version-database/</link>
		<comments>http://www.jameswiseman.com/blog/2010/09/08/sql-server-database-version-database/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 11:30:44 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Versions]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=1396</guid>
		<description><![CDATA[Yes, its out there on the web; a SQL Server Database Version Database. Or, to put it differently, a Database of SQL Server Versions. Here  you can find a list of all the versions of SQL Server that have ever been released. It even goes right back to version 6.0, released in 1995. Check out [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Yes, its out there on the web; <a title="SQL Server Version Database" href="http://www.sqlsecurity.com/FAQs/SQLServerVersionDatabase/tabid/63/Default.aspx" target="_blank">a SQL Server Database Version Database</a>. Or, to put it differently, a Database of SQL Server Versions.</p>
<p>Here  you can find a list of all the versions of SQL Server that have ever been released. It even goes right back to version 6.0, released in 1995. Check out the <a title="SQL Server Release History" href="http://en.wikipedia.org/wiki/Microsoft_SQL_Server#Genesis" target="_blank">SQL Server Release History</a> if you are interested.</p>
<p>I was drawn to this list when I had two database connections open in my object explorer:</p>
<blockquote><p>SQLDatabase01 (SQL Server 8.0.2050 &#8211; Context/UserName)</p>
<p>SQLDatabase02 (SQL Server 8.0.2039 &#8211; Context/UserName)</p></blockquote>
<p>I was curious as to what the difference was, so investigated further and found the <a title="SQL Server Version Database" href="http://www.sqlsecurity.com/FAQs/SQLServerVersionDatabase/tabid/63/Default.aspx" target="_blank"> SQL Server Version Database</a>. I found the information I wanted with ease:</p>
<blockquote><p>2000 (GDR) SP4+Q941203 / <a href="http://support.microsoft.com/?Kbid=948110" target="_blank">948110</a> &#8211; 8.0.2050</p>
<p>2000  SP4 &#8211; 8.0.2039</p></blockquote>
<p>And looking at the hundreds of different versions available, its not surprising that something like this is needed.</p>
<div class="shr-publisher-1396"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F09%2F08%2Fsql-server-database-version-database%2F' data-shr_title='SQL+Server+Database+Version+Database'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F09%2F08%2Fsql-server-database-version-database%2F' data-shr_title='SQL+Server+Database+Version+Database'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/09/08/sql-server-database-version-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Indentity Inserting in SQL Server</title>
		<link>http://www.jameswiseman.com/blog/2010/08/24/indentity-inserting-in-sql-server/</link>
		<comments>http://www.jameswiseman.com/blog/2010/08/24/indentity-inserting-in-sql-server/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 18:00:48 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Identity Insert]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=1325</guid>
		<description><![CDATA[When I starting writing this blog, I was a little unsure as to what to put in it. I knew I wanted it to be mostly programming related, with articles about things that interested me. So I named it my &#8216;Programming Notebook&#8217; and put into words exactly what I envisaged I&#8217;d be putting in it. [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>When I starting writing this blog, I was a little unsure as to what to put in it. I knew I wanted it to be mostly programming related, with articles about things that interested me. So I named it my &#8216;Programming Notebook&#8217; and put into words exactly what I envisaged I&#8217;d be putting in it. This is what I put in my <a title="About page" href="http://www.jameswiseman.com/blog/about/" target="_blank">About page</a>:</p>
<blockquote><p>My main intention was for this blog was to be a notebook for myself in a presentable format – an aid to memory of things I’ve found useful on a day-to-day basis, and little snippets I’d like to share. A lot of it won’t be ground-breaking and you’ll almost certainly find it around elsewhere on the net, but it’s presented in a way that I learned it in the hope that others might find it useful.</p></blockquote>
<p>And later on I started fretting that I was duplicating what already &#8216;out there&#8217;, which is nonsense, because almost everything that anyone writes is already &#8216;out there&#8217; anyway.</p>
<p>None of this has anything to do with Identity Inserting in SQL Server, but maybe goes some way to explaining why I am writing about something that is well documented &#8216;out there&#8217;.</p>
<p>The reason is that I had to look it up twice. I needed to look it once, and then I forgot, so I needed to look it up again. What I needed was some sort of programming noteb&#8230;. hey&#8230; I have one!</p>
<p>So here it is:</p>
<pre name="code" class="sql">SET IDENTITY_INSERT [MyTable] ON
INSERT INTO [MyTable] (IDField, Field2, Field3, Field4) values (1,212,2,2)
SET IDENTITY_INSERT [MyTable] OFF</pre>
<p>You&#8217;ll need to set IDENTITY_INSERT &#8216;ON&#8217; for the table into which you are inserting, and (for good measure) set it back to &#8216;OFF&#8217; when you&#8217;re done.</p>
<p>You&#8217;ll also need to include a field list (which is good practice anyway). The following just won&#8217;t work:</p>
<pre name="code" class="sql">INSERT INTO [MyTable] values (1,212,2,2)</pre>
<p>So there we are, SQL Server Identity Inserting covered&#8230;.again.</p>
<div class="shr-publisher-1325"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F08%2F24%2Findentity-inserting-in-sql-server%2F' data-shr_title='Indentity+Inserting+in+SQL+Server'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F08%2F24%2Findentity-inserting-in-sql-server%2F' data-shr_title='Indentity+Inserting+in+SQL+Server'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/08/24/indentity-inserting-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Heirarchy Sorting in SQL Server</title>
		<link>http://www.jameswiseman.com/blog/2010/08/16/heirarchy-sorting-in-sql-server/</link>
		<comments>http://www.jameswiseman.com/blog/2010/08/16/heirarchy-sorting-in-sql-server/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 18:01:43 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Hierarchy]]></category>
		<category><![CDATA[Sorting]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Stackoverflow]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=1276</guid>
		<description><![CDATA[One of the reasons I like Stackoverflow as a place for asking and answering programming questions is that I Almost always get what I am after Almost always learn something in the process (in answering and asking) Case in point with a question I asked regarding hierarchy sorting in SQL Server. The question in full [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>One of the reasons I like <a title="stackoverflow.com" href="http://www.stackoverflow.com" target="_blank">Stackoverflow </a>as a place for asking and answering programming questions is that I</p>
<ol>
<li>Almost always get what I am after</li>
<li>Almost always learn something in the process (in answering and asking)</li>
</ol>
<p style="padding-left: 30px;"><iframe height="60" width="210" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" src="http://stackoverflow.com/users/flair/144491.html?theme=clean"></iframe></p>
<p>Case in point with a <a title="Stackoverflow hierarchy Sorting" href="http://stackoverflow.com/q/3474870/144491" target="_blank">question I asked regarding hierarchy sorting in SQL Server</a>. The question in full can be found on the adjacent link, but, in a nutshell I was looking at how I might sort a variable length hierarchy field in SQL Server 2000.</p>
<p>So, the field value could be 1. Or it could be 1.2.3.4. Or it could be 1.92.333.14.58.62.7.81231.</p>
<p>Given that I needed to sort by the last digit, then the second-last, etc, etc, AND that the length was variable, I found myself a little stumped as to an elegant solution. So I decided to call upon the generosity of the Stackoverflow community</p>
<p>First-up was a number of fixed length solutions, which while good, didn&#8217;t quite help. Then I got some help in SQL 2008, which was unfortunate, as I was wanting it in SQL 2000. And then user <a title="Stackoverflow User Vash" href="http://stackoverflow.com/users/390695/vash" target="_blank">Vash</a> popped up with an edit to his original post:</p>
<p style="padding-left: 30px;"><iframe height="60" width="210" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" src="http://stackoverflow.com/users/flair/390695.html?theme=clean"></iframe></p>
<blockquote><p>Solutions for 2000, 2005, 2008: <a title="Solutions to T-SQL Sorting Challenge here" rel="nofollow" href="http://www.sqlmag.com/article/sql-server/solutions-to-t-sql-sorting-challenge.aspx" target="_blank">Solutions to T-SQL Sorting Challenge here</a>.</p></blockquote>
<p>Marvellous! Here is the solution (modified sightly) that I adopted:</p>
<pre name="code" class="sql">CREATE FUNCTION dbo.ufn_SortByHierarchy(@s as varchar(500), @delimiter as char(1))
  returns VARBINARY(2500)
AS
BEGIN
  DECLARE @pos as int, @r as varbinary(2500), @element as int;

  SET @r = 0x;
  SET @s = @s + @delimiter;
  SET @pos = charindex(@delimiter, @s);
  while @pos &gt; 0
  BEGIN
	SET @element = cast(left(@s, @pos - 1) AS INT);

	SET @r = @r +
	  CASE
		WHEN @element = -2147483648 THEN 0x0000000000
		WHEN sign(@element) = -1
		  THEN 0x01 + cast(2147483647 + @element AS BINARY(4))
		ELSE 0x02 + cast(@element AS BINARY(4))
	  END;

	SET @s = stuff(@s, 1, @pos, '');
	SET @pos = charindex(@delimiter, @s);
  END

  RETURN @r;

END</pre>
<p>And, as it turned out, I&#8217;d never heard of the <strong>varbinary</strong> data type, so right there was some extra reading and knowledge for me.</p>
<p>And that, I thought was it. But no! A short while later, user <a title="Stackoverflow User Martin Smith" href="http://stackoverflow.com/users/73226/martin-smith" target="_blank">Martin Smith</a>, popped up with a comment.</p>
<p style="padding-left: 30px;"><iframe height="60" width="210" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" src="http://stackoverflow.com/users/flair/73226.html?theme=clean"></iframe></p>
<blockquote><p>@James &#8211; Not sure what you ended up using. You can also use <code>parsename</code> for this as in this answer: <a title="how to get max value of a version number varchar column in t sql" rel="nofollow" href="http://stackoverflow.com/q/3057532/144491">stackoverflow.com/questions/3057532/</a></p></blockquote>
<p>A comment! I&#8217;d have voted it up if it were an answer. So I followed the link, and voted that up.</p>
<p>So, I also learned about <a title="SQL Server Parsename" href="http://msdn.microsoft.com/en-us/library/ms188006.aspx" target="_blank">Parsename</a>, which works in lowly SQL 2000 as well. unfortunately, it only works on a four-digit hierarchy, but still useful for me to know about.</p>
<p>So, lots of useful information, new things learned and a permanently documented solution to my problem. That is why I like Stackoverflow.</p>
<div class="shr-publisher-1276"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F08%2F16%2Fheirarchy-sorting-in-sql-server%2F' data-shr_title='Heirarchy+Sorting+in+SQL+Server'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F08%2F16%2Fheirarchy-sorting-in-sql-server%2F' data-shr_title='Heirarchy+Sorting+in+SQL+Server'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/08/16/heirarchy-sorting-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Never Use &#8216;Select *&#8217; To Return Data</title>
		<link>http://www.jameswiseman.com/blog/2010/07/23/never-use-select-star-to-return-data/</link>
		<comments>http://www.jameswiseman.com/blog/2010/07/23/never-use-select-star-to-return-data/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 10:59:42 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=1125</guid>
		<description><![CDATA[Not in production environment, anyway! Recently, I have been involved in implementing functionality that requires a modification to the structure of a SQL Server table. In fact, the nature of the change was a simple addition of a new column to the table in question. Logic would dictate that that wouldn&#8217;t be too problematic. Right? [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Not in production environment, anyway!</p>
<p>Recently, I have been involved in implementing functionality that requires a modification to the structure of a SQL Server table. In fact, the nature of the change was a simple  addition of a new column to the table in question.</p>
<p>Logic would dictate that that wouldn&#8217;t be too problematic. Right? Wrong!</p>
<p>The fact is, that when you use &#8216;*&#8217; to reference all columns on a table, you&#8217;ll make it impossible to perform any reliable impact analysis on your code base. Columns will be used without being explicitly referenced.</p>
<p>If you add more columns to the table in future, who can guarantee that your existing views or stored procedures will actually need them? If you have code in them that performs a &#8216;Select *&#8217; then they will, regardless. The obvious problem with this is the overhead of returning unwanted data from the database, and the potential degradation of performance inherent therein. However, there is another pitfall surrounding table joins, which I will now attempt to demonstrate:</p>
<p>Imagine you start out with two tables:</p>
<pre class="sql" name="code">create table Test (Id int, code char(1))
create table Test2 (Id int, TestId int, name varchar(10))</pre>
<p>And you would like a query that joins these two, so you create a view</p>
<pre class="sql" name="code">create view vwJoinTests as
select t.code, t2.*
from test t
inner join test2 t2 on t.id = t2.testid</pre>
<p>You can select from this, no problem</p>
<pre class="sql" name="code">select code from vwJoinTests</pre>
<p>A year later, another developer comes along and adds a new column to Test2:</p>
<pre class="sql" name="code">alter table Test2 add code char(1)</pre>
<p>Now, the column &#8216;Code&#8217; exists on both tables.</p>
<p>Selecting from the view is still fine:</p>
<pre class="sql" name="code">select code from vwJoinTests</pre>
<p>And, because our original &#8216;select&#8217; remains intact (it doesn&#8217;t break anything), this fault will not be picked up by testing.</p>
<p>But, if I want to reapply the view code, I get the following error:<br />
<strong>Column names in each view or function must be unique. Column name &#8216;code&#8217; in view or function &#8216;vwJoinTests&#8217; is specified more than once.</strong></p>
<p>Why would I want to reapply the view? Well:</p>
<ol>
<li>You may want to modify it</li>
<li>You may want to rebuild a database</li>
<li>You may want to install a brand new database on a client site</li>
</ol>
<p>Technically, 2 and 3 are the same, but I thought I&#8217;d emphasise the potential for embarrasment when it comes to your clients.</p>
<p>It also emphasises the point that structural changes to the database, should be accompanied by a full database build test to ensure its integrity has been maintained.</p>
<div class="shr-publisher-1125"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F07%2F23%2Fnever-use-select-star-to-return-data%2F' data-shr_title='Never+Use+%27Select+%2A%27+To+Return+Data'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F07%2F23%2Fnever-use-select-star-to-return-data%2F' data-shr_title='Never+Use+%27Select+%2A%27+To+Return+Data'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/07/23/never-use-select-star-to-return-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic SQL</title>
		<link>http://www.jameswiseman.com/blog/2010/07/01/dynamic-sql/</link>
		<comments>http://www.jameswiseman.com/blog/2010/07/01/dynamic-sql/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 16:20:04 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Dynamic SQL]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Static SQL]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=799</guid>
		<description><![CDATA[Dynamic SQL is, by its nature, very flexible and often very useful. However, it is this flexibility that can be both a blessing and a curse. The offical Oracle recommendations on Static vs Dynamic SQL state that, in practice, &#8220;static SQL will meet nearly all your programming needs.&#8221; They go on to give a number of [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Dynamic SQL is, by its nature, very flexible and often very useful. However, it is this flexibility that can be both a blessing and a curse.</p>
<p>The offical <a title="Oracle Dynamic SQL Recommendations" href="http://download.oracle.com/docs/cd/B10501_01/appdev.920/a42525/ch10.htm#toc109" target="_blank">Oracle recommendations on Static vs Dynamic SQL</a> state that, in practice, &#8220;static SQL will meet nearly all your programming needs.&#8221;<a href="http://www.jameswiseman.com/blog/wp-content/uploads/2010/07/dunamicsql.jpg"><img class="alignright size-full wp-image-944" title="dunamicsql" src="http://www.jameswiseman.com/blog/wp-content/uploads/2010/07/dunamicsql.jpg" alt="Dynamic SQL" width="226" height="127" /></a></p>
<p>They go on to give a number of conditions under which they deem it acceptable for you to use dynamic SQL. They suggest it is used when one or more of the following items is unknown at precompile time:</p>
<ul>
<li>text of the SQL statement (commands, clauses, and so on)</li>
<li>the number of host variables</li>
<li>the datatypes of host variables</li>
<li>references to database objects such as columns, indexes, sequences, tables, usernames, and views</li>
</ul>
<p>Although this may be true, I would prefer to add the caveat <em>&#8220;Only if you absolutely cannot find a way to do it in Static SQL&#8221;</em></p>
<p>They also state:</p>
<blockquote><p>However, some dynamic queries require complex coding, the use of special data structures, and more runtime processing. While you might not notice the added processing time, you might find the coding difficult unless you fully understand dynamic SQL concepts and methods.</p></blockquote>
<p>So rather than have a long and tedious discussion as to the relative merits, lets just list the advantages and disadvantages in a series of bullet points.</p>
<p><strong>Advantages</strong></p>
<ul>
<li>It gives flexibility and scalability</li>
<li>It can reduce the number of lines of code written</li>
</ul>
<p><strong>Disadvantages</strong></p>
<ul>
<li>It can become very complex and difficult to read. Think about quotes embedded in quotes, and other such things.</li>
<li>It can have a detrimental effect on code stability. Some Dynamic SQL errors will not be known until run time. (An example of this is where you reference a non-existent table)</li>
<li>Dynamic SQL code is harder to test than the equivalent static SQL. It may also be impossible to test for every possible circumstance that your Dynamic SQL will encounter, thus introducing inherent risk.</li>
<li>It will be more difficult to conduct an effective impact analysis on Dynamic SQL in your code-base.</li>
<li>SQL injection and misuse &#8211; Dynamic SQL is more prone to misuse, and is invariably less safe than static SQL</li>
<li>The queries code within Dynamic SQL is not subject to a query plan, and as such optimisations may be missed. As such, it can be slower than the equivalent static SQL</li>
<li>As the SQL query is not known until runtime, it can be harder to performance-tune SQL Dynamic code (for example, determining the indexes that might be required on a table)</li>
</ul>
<p>So there we are. Take that and pin in on a wall somewhere <img src='http://www.jameswiseman.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<div class="shr-publisher-799"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F07%2F01%2Fdynamic-sql%2F' data-shr_title='Dynamic+SQL'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F07%2F01%2Fdynamic-sql%2F' data-shr_title='Dynamic+SQL'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/07/01/dynamic-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VB6? But I Really Wanted To Use C#!</title>
		<link>http://www.jameswiseman.com/blog/2010/06/01/vb6-but-i-really-wanted-to-use-c/</link>
		<comments>http://www.jameswiseman.com/blog/2010/06/01/vb6-but-i-really-wanted-to-use-c/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 18:28:36 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[VB6]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=747</guid>
		<description><![CDATA[Programmers like new toys that they can play with, be it a second monitor, a new IDE, or a new programming language. They also like to feel that they are learning new things and not getting assigned to the technological scrap-heap and so will inherently favour (for example) C#3 above VB6 even when the final [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Programmers like new toys that they can play with, be it a second monitor, a new IDE, or a new programming language. They also like to feel that they are learning new things and not getting assigned to the technological scrap-heap and so will inherently favour (for example) C#3 above VB6 even when the final solution might be suboptimal.<a href="http://www.jameswiseman.com/blog/wp-content/uploads/2010/06/csharp.png"><img class="size-medium wp-image-754 alignright" title="csharp" src="http://www.jameswiseman.com/blog/wp-content/uploads/2010/06/csharp-300x300.png" alt="C#" width="150" height="150" /></a></p>
<p>I found myself drifting into this mindset recently when prototyping a design for component to fit in with an existing framework of COM components.</p>
<p>The component needed to call out to SQL Server 2005 to start an Agent Job. My first instinct was that I had to interface with SQL 2005 Native Client components through the .NET libraries. I envisaged something something like this:</p>
<pre name="code" class="c#">//...get the connectionString somehow...
string connectionString = "Data Source=SOMESERVER;Initial Catalog=SomeDatabase;Integrated Security=SSPI;Provider=SQLNCLI.1;";
OleDbConnection oleDbConnection = new OleDbConnection(connectionString);
ServerConnection serverConnection = new ServerConnection(oleDbConnection.DataSource);
Server oSqlServer = new Server(serverConnection);

JobServer oAgent = oSqlServer.JobServer;
Job oJob = oAgent.Jobs["Test"];
JobHistoryFilter oFilter = new JobHistoryFilter();
oFilter.JobName = "Test";
oJob.Start();
</pre>
<p>&#8230;or something similar therein.</p>
<p>The component would have to <a title="Expose a COM Interface and be registered using REGASM" href="http://www.jameswiseman.com/blog/2010/04/27/exposing-a-com-interface-from-a-net-type-library/" target="_blank">expose a COM Interface and be registered using REGASM</a>, which would requrie manual intervension in the shipping process.</p>
<p>In addition to this, I would have neededed to organise for the SQL 2005 Client tools installed on four servers and undertake regression testing of the applications on these servers to inspire confidence that this hadn&#8217;t broken anything.</p>
<p>To be frank, it all seemed a little onerous and overcomplicated. Apart from simply wanting to use C#, the reason for this doing this <a href="http://www.jameswiseman.com/blog/wp-content/uploads/2010/06/vb6.gif"><img class="alignleft size-medium wp-image-756" title="vb6" src="http://www.jameswiseman.com/blog/wp-content/uploads/2010/06/vb6-300x229.gif" alt="vb6" width="150" height="115" /></a>was based on an assumption that I needed the .NET library code to hook into SQL Server and that there was no other way. Really? Have you ever heard such nonsense?</p>
<p>Without realising, I&#8217;d very nearly falling into the <a title="Second System Effect" href="http://en.wikipedia.org/wiki/Second-system_effect" target="_blank">Second System Effect</a> trap (of sorts). All I really had to do was make a call out to the SQL Server<strong> <a title="sp_start_job" href="http://msdn.microsoft.com/en-us/library/ms186757.aspx" target="_blank"><span style="font-weight: normal;">sp_start_job</span></a></strong> to achieve the same effect, and all I needed was trusty old <a title="MDAC 2.8" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=6c050fe3-c795-4b7d-b037-185d0506396c&amp;displaylang=en" target="_blank">MDAC 2.8</a>. This meant I could write it in VB6 to fit in with existing framework components with none of the impact or complication inherent with the C#-developed component.</p>
<p>So, I set about writing a little test application as proof of concept. It looked something like this:</p>
<pre  name="code" class="vb">
Private Sub Command1_Click()
    Dim connection As New ADODB.Connection
    Dim connectionString As String

    connectionString = "Data Source=SOMESERVER;Initial Catalog=SomeDatabase;Integrated Security=SSPI;Provider=SQLOLEDB.1;"

    Call connection.Open(connectionString)

    connection.Execute ("EXEC msdb.dbo.sp_start_job @job_name=N'Test'")

    MsgBox ("Success")
End Sub
</pre>
<p>But I really wanted to use C#! However,  in this instance it just wasn&#8217;t feasible.</p>
<p>So, in conclusion, don&#8217;t get blinded by shiny new toys or by a desire to improve oneself, don&#8217;t simply dive into the first solution that you come across, and remember, complication is ruination!</p>
<div class="shr-publisher-747"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F06%2F01%2Fvb6-but-i-really-wanted-to-use-c%2F' data-shr_title='VB6%3F+But+I+Really+Wanted+To+Use+C%23%21'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F06%2F01%2Fvb6-but-i-really-wanted-to-use-c%2F' data-shr_title='VB6%3F+But+I+Really+Wanted+To+Use+C%23%21'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/06/01/vb6-but-i-really-wanted-to-use-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle 11g vs SQL Server 2008 Report</title>
		<link>http://www.jameswiseman.com/blog/2010/05/27/oracle-11g-vs-sql-server-2008-report/</link>
		<comments>http://www.jameswiseman.com/blog/2010/05/27/oracle-11g-vs-sql-server-2008-report/#comments</comments>
		<pubDate>Thu, 27 May 2010 11:30:42 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=656</guid>
		<description><![CDATA[This was brought to my attention today, and made for interesting reading: Oracle Database 11g vs. Microsoft SQL Server 2008 The report claims in its &#8216;Executive Summary&#8217; that Oracle 11g holds a substantial advantage over SQL Server, and alleges the following high level points: Oracle is faster for DBAs to perform administrative functions Oracle has [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>This was brought to my attention today, and made for interesting reading:</p>
<p><a title="Oracle Database 11g vs.Microsoft SQL Server 2008" href="http://www.oracle.com/us/products/database/039433.pdf" target="_blank">Oracle Database 11g vs. Microsoft SQL Server 2008</a><a href="http://www.jameswiseman.com/blog/wp-content/uploads/2010/05/11gR2.png"><img class="alignright size-medium wp-image-659" title="11gR2" src="http://www.jameswiseman.com/blog/wp-content/uploads/2010/05/11gR2-300x171.png" alt="Oracle 11g" width="300" height="171" /></a></p>
<p>The report claims in its &#8216;Executive Summary&#8217; that Oracle 11g holds a substantial advantage over SQL Server, and alleges the following high level points:</p>
<ul>
<li>Oracle is faster for DBAs to perform      administrative functions</li>
<li>Oracle has fewer steps for standards RDBMS      tasks</li>
<li>Increased Oracle DBA productivity due to      lower complexity and higher efficiency</li>
<li>Oracle has architectural and functional      capabilities for backup and recovery beyond those offered by SQL Server</li>
<li>Oracle demonstrates significant time savings      with regard to performance diagnostics and tuning</li>
</ul>
<p>Later in the Rationale section, however is the following statement:</p>
<blockquote><p>Oracle has sponsored the studies in response to a challenge made by Edison to Oracle claims that Oracle Database was significantly easier and thus less costly to manage than the other major database management systems.</p></blockquote>
<p>Which, of course, makes me suspicious. Regardless of Edison&#8217;s so-called &#8216;challenge&#8217;, the report is still sponsored by Oracle (and published on their web site), and I would have been surprised if this came back in SQL Server&#8217;s favour.</p>
<p>Predictably, <a title="Microsoft's Response to Edison Report" href="http://webcache.googleusercontent.com/search?q=cache:_5r7NIrU_PQJ:download.microsoft.com/download/A/F/1/AF1CC0A9-05B0-46FB-ACE3-2E4B11F8D182/OracleEdisonResponse.docx&amp;cd=4&amp;hl=en&amp;ct=clnk&amp;gl=uk" target="_blank">Microsoft has published a response</a> denouncing these claims. It alleges the following: <a href="http://www.jameswiseman.com/blog/wp-content/uploads/2010/05/sql_server_2008_logo.png"><img class="alignright size-medium wp-image-660" title="sql_server_2008_logo" src="http://www.jameswiseman.com/blog/wp-content/uploads/2010/05/sql_server_2008_logo-300x187.png" alt="SQL Server 2008" width="300" height="187" /></a></p>
<ul>
<li>The report has unverifiable metrics and      inconsistent interpretation of results</li>
<li>The report is not based on &#8216;real-world; data</li>
<li>SQL Licensing Costs are cheaper</li>
<li>Oracle databases consistently suffer from more      security vulnerabilities</li>
<li>Security patching not reflected in true cost      of operating Oracle 11g</li>
</ul>
<p>I&#8217;ll not pass comment, but thought it interesting to share!</p>
<div class="shr-publisher-656"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F05%2F27%2Foracle-11g-vs-sql-server-2008-report%2F' data-shr_title='Oracle+11g+vs+SQL+Server+2008+Report'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F05%2F27%2Foracle-11g-vs-sql-server-2008-report%2F' data-shr_title='Oracle+11g+vs+SQL+Server+2008+Report'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/05/27/oracle-11g-vs-sql-server-2008-report/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Identify Your SQL Server Version and Edition</title>
		<link>http://www.jameswiseman.com/blog/2010/05/18/how-to-identify-your-sql-server-version-and-edition/</link>
		<comments>http://www.jameswiseman.com/blog/2010/05/18/how-to-identify-your-sql-server-version-and-edition/#comments</comments>
		<pubDate>Mon, 17 May 2010 23:00:23 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=482</guid>
		<description><![CDATA[I&#8217;ve been checking whether or not service packs have been applied on SQL Server Installations. I keep forgetting the script that I run to check this,  and having to look it up. So, here it is: SELECT SERVERPROPERTY(&#8216;productversion&#8217;), SERVERPROPERTY (&#8216;productlevel&#8217;), SERVERPROPERTY (&#8216;edition&#8217;) This seems to work in SQL 2000, 2005 and 2008 Taken from this [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I&#8217;ve been checking whether or not service packs have been applied on SQL Server Installations. I keep forgetting the script that I run to check this,  and having to look it up.</p>
<p>So, here it is:</p>
<p><span style="font-family: monospace;"><span style="color: #0000ff;">SELECT </span><span style="color: #ff00ff;">SERVERPROPERTY</span>(<span style="color: #ff0000;">&#8216;productversion&#8217;</span>), <span style="color: #ff00ff;">SERVERPROPERTY </span>(<span style="color: #ff0000;">&#8216;productlevel&#8217;</span>), <span style="color: #ff00ff;">SERVERPROPERTY </span>(<span style="color: #ff0000;">&#8216;edition&#8217;</span>)</span></p>
<p>This seems to work in SQL 2000, 2005 and 2008</p>
<p>Taken from this Microsoft link:</p>
<p><a href="http://support.microsoft.com/kb/321185" target="_blank">http://support.microsoft.com/kb/321185</a></p>
<p>You could also try</p>
<p><code><span style="color: #0000ff;">SELECT </span><span style="color: #ff00ff;">@@version</span><br />
</code></p>
<p>Which will give you everything (and more) in a single string:</p>
<p><code>Microsoft SQL Server 2005 - 9.00.3042.00 (Intel X86)   Feb  9 2007 22:47:07   Copyright (c) 1988-2005 Microsoft Corporation  Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 2)</code></p>
<div class="shr-publisher-482"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F05%2F18%2Fhow-to-identify-your-sql-server-version-and-edition%2F' data-shr_title='How+To+Identify+Your+SQL+Server+Version+and+Edition+'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F05%2F18%2Fhow-to-identify-your-sql-server-version-and-edition%2F' data-shr_title='How+To+Identify+Your+SQL+Server+Version+and+Edition+'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/05/18/how-to-identify-your-sql-server-version-and-edition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Free EBook on Defensive Programming in SQL Server</title>
		<link>http://www.jameswiseman.com/blog/2010/05/16/free-ebook-on-defensive-programming-in-sql-server/</link>
		<comments>http://www.jameswiseman.com/blog/2010/05/16/free-ebook-on-defensive-programming-in-sql-server/#comments</comments>
		<pubDate>Sat, 15 May 2010 23:00:02 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Defensive Programming]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=483</guid>
		<description><![CDATA[Go nuts! Here is a free EBook on Defensive SQL Server Programming from Red Gate Defensive Database Programming with SQL Server Alex Kuznetsov ISBN: 978-1-906434-44-1 This leads us to another interesting article: The Curse and Blessings of Dynamic SQL]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Go nuts! Here is a free EBook on Defensive SQL Server Programming from <a title="Red Gate" href="http://www.red-gate.com">Red Gate</a></p>
<p><strong><a title="Defensive Database Programming with SQL Server" href="http://www.red-gate.com/specials/Ebooks/Defensive_Database_Programming.pdf" target="_blank">Defensive Database Programming with SQL Server</a></strong></p>
<p><strong>Alex Kuznetsov</strong></p>
<p><strong>ISBN: 978-1-906434-44-1</strong></p>
<p>This leads us to another interesting article:</p>
<p><a title="The Curse and Blessings of Dynamic SQL" href="http://www.sommarskog.se/dynamic_sql.html" target="_blank">The Curse and Blessings of Dynamic SQL</a></p>
<div class="shr-publisher-483"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F05%2F16%2Ffree-ebook-on-defensive-programming-in-sql-server%2F' data-shr_title='Free+EBook+on+Defensive+Programming+in+SQL+Server'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F05%2F16%2Ffree-ebook-on-defensive-programming-in-sql-server%2F' data-shr_title='Free+EBook+on+Defensive+Programming+in+SQL+Server'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/05/16/free-ebook-on-defensive-programming-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fast Row Counting in SQL Server</title>
		<link>http://www.jameswiseman.com/blog/2010/05/04/fast-row-counting-in-sql-server/</link>
		<comments>http://www.jameswiseman.com/blog/2010/05/04/fast-row-counting-in-sql-server/#comments</comments>
		<pubDate>Tue, 04 May 2010 08:00:35 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=326</guid>
		<description><![CDATA[I love it when I discover something like this&#8230; I&#8217;m querying a table in SQL Server that has over 200 million rows. I&#8217;m just wanting the row count. So, naturally, I tried the following: select count(*) from TABLE_NAME The query took one minute when it was run initially. And select (1) performed no better. (See: SQL [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I love it when I discover something like this&#8230;</p>
<p>I&#8217;m querying a table in SQL Server that has over 200 million rows. I&#8217;m just wanting the row count. So, naturally, I tried the following:</p>
<p><code><span style="color: #0000ff;"> select </span><span style="color: #ff00ff;">count</span>(<span style="color: #999999;">*</span>) <span style="color: #0000ff;">from <span style="color: #000000;">TABLE_NAME</span></span></code></p>
<p><code> </code></p>
<p>The query took one minute when it was run initially. And select (1) performed no better. (See: <a title="SQL count(1) is not better than count(*)" href="http://www.jameswiseman.com/blog/wp-admin/post.php?action=edit&amp;post=42" target="_blank">SQL count(1) is not better than count(*)</a>)</p>
<p>To be fair, I was quite shocked that it took so long to get this value. Surely there was some value stored internally that I could just pluck out in the blink of an eye?</p>
<p>I searched around for this information, and stumbled across this: <a title="How can I get sysindexes information in Sql 2005" href="http://blogs.msdn.com/chadboyd/archive/2007/02/24/how-can-i-get-sysindexes-information-in-sql-2005.aspx" target="_blank">How can I get sysindexes information in Sql 2005?</a></p>
<p>So here it is. Takes no time at all to run:</p>
<p><span style="font-family: monospace;"><span style="color: #0000ff;">select </span><span style="color: #ff00ff;">o</span><span style="color: #ff00ff;">bject_name</span>(i.<span style="color: #ff00ff;">object_id</span>) <span style="color: #0000ff;">as </span>objectName, <span style="color: #ff00ff;">sum</span>(p.rows) <span style="color: #0000ff;">as </span>rowCnt, i.name <span style="color: #0000ff;">as </span>indexName</span><br />
<code> <span style="color: #0000ff;"> from </span><span style="color: #008000;">sys.indexes</span> i<br />
<span style="color: #808080;"> join </span><span style="color: #008000;">sys.partitions</span> p <span style="color: #0000ff;">on </span> i.<span style="color: #ff00ff;">object_id </span>= p.<span style="color: #ff00ff;">object_id</span><br />
<span style="color: #808080;">and </span> i.index_id = p.index_id<br />
<span style="color: #0000ff;"> where </span>i.index_id &lt;= 1<br />
<span style="color: #808080;"> and </span>i.<span style="color: #ff00ff;">object_id </span>= <span style="color: #ff00ff;">object_id</span>(<span style="color: #ff0000;">'TABLE_NAME</span>')<br />
<span style="color: #0000ff;">group by</span> i.<span style="color: #ff00ff;">object_id</span>, i.index_id, i.name<span style="color: #0000ff;"><br />
</span></code></p>
<p>And, if I need, I can strip out the second-last line to get it for all tables.</p>
<p>As it turns out, you can do the equivalent in SQL Server 2000:</p>
<p><code><span style="color: #0000ff;">select </span><span style="color: #ff00ff;">object_name</span>(id), rowcnt<br />
<span style="color: #0000ff;"> from </span><span style="color: #008000;">sysindexes</span><br />
<span style="color: #0000ff;"> where </span>indid &lt; 2<br />
<span style="color: #808080;"> and </span>id = <span style="color: #ff00ff;">object_id</span>(<span style="color: #ff0000;">'TABLE_NAME'</span>)<br />
<span style="color: #0000ff;"> order by</span> 2</code></p>
<p>Which will run in 2005 as well, although its legacy, so we must discourage that! You can tell this, because SQL Management Studio doesn&#8217;t colour in <strong>sysindexs</strong> green, but SQL 2000 Query Analyzer does.</p>
<div class="shr-publisher-326"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F05%2F04%2Ffast-row-counting-in-sql-server%2F' data-shr_title='Fast+Row+Counting+in+SQL+Server'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F05%2F04%2Ffast-row-counting-in-sql-server%2F' data-shr_title='Fast+Row+Counting+in+SQL+Server'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/05/04/fast-row-counting-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	<img style='margin:0;padding:0;border:0;' width='1px' height='1px' src="http://www.jameswiseman.com/blog/wp-content/plugins/mystat/mystat.php?act=time_load&id=617040&rnd=336007842" /></channel>
</rss>

