<?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>Always Get Better &#187; SQL</title>
	<atom:link href="http://www.alwaysgetbetter.com/blog/category/general-programming/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alwaysgetbetter.com/blog</link>
	<description>Never stop looking for ways to improve</description>
	<lastBuildDate>Wed, 02 Jun 2010 12:16:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>VISTA: How to fix SQL Server Express Error &#8211; CREATE DATABASE permission denied in database &#8216;master&#8217;</title>
		<link>http://www.alwaysgetbetter.com/blog/2009/11/05/vista-fix-sql-server-express-error-create-database-permission-denied-database-master/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2009/11/05/vista-fix-sql-server-express-error-create-database-permission-denied-database-master/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 13:22:16 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Windows Vista]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=265</guid>
		<description><![CDATA[If you&#8217;re using SQL Server Management Studio Express under Windows Vista and see either of these errors:
CREATE DATABASE permission denied in database 'master'
or
The database [Name] is not accessible. (Microsoft.SqlServer.Express.ObjectExplorer)
Here&#8217;s the fix:

Close SQL Server Management Studio Express
Open your start menu and locate that program.
Right-click on the Management Studio and choose &#8216;Run as Administrator&#8217;
Fixed!

I swear the simplest [...]


Related posts:<ol><li><a href='http://www.alwaysgetbetter.com/blog/2010/01/24/wamp-server-crashes-installing-xcart/' rel='bookmark' title='Permanent Link: Wamp Server Crashes Installing X-Cart'>Wamp Server Crashes Installing X-Cart</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re using SQL Server Management Studio Express under Windows Vista and see either of these errors:</p>
<p><code>CREATE DATABASE permission denied in database 'master'</code></p>
<p>or</p>
<p><code>The database [Name] is not accessible. (Microsoft.SqlServer.Express.ObjectExplorer)</code></p>
<p>Here&#8217;s the fix:</p>
<ol>
<li>Close SQL Server Management Studio Express</li>
<li>Open your start menu and locate that program.</li>
<li>Right-click on the Management Studio and choose &#8216;Run as Administrator&#8217;</li>
<li>Fixed!</li>
</ol>
<p>I swear the simplest solutions can be the hardest to find &#8211; hopefully this saves someone (or my forgetful self!) some aggravation.</p>


<p>Related posts:<ol><li><a href='http://www.alwaysgetbetter.com/blog/2010/01/24/wamp-server-crashes-installing-xcart/' rel='bookmark' title='Permanent Link: Wamp Server Crashes Installing X-Cart'>Wamp Server Crashes Installing X-Cart</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2009/11/05/vista-fix-sql-server-express-error-create-database-permission-denied-database-master/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Optimize SQL Queries by Using Aliases</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/06/30/optimize-sql-queries-by-using-aliases/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/06/30/optimize-sql-queries-by-using-aliases/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 02:22:23 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[efficiency]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=35</guid>
		<description><![CDATA[When joining tables in SQL, we often use aliases to shorten table names.  Consider this query joining order lines (details) with orders inside the database:
SELECT O.OrderID, FirstName, LastName, ItemName, Quantity
FROM Orders O
INNER JOIN OrderDetails OD
ON O.OrderID = OD.OrderID;
The above may work, but behind the scenes the database&#8217;s query analyzer has to associate each of [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>When joining tables in SQL, we often use aliases to shorten table names.  Consider this query joining order lines (details) with orders inside the database:</p>
<p>SELECT O.OrderID, FirstName, LastName, ItemName, Quantity<br />
FROM Orders O<br />
INNER JOIN OrderDetails OD<br />
ON O.OrderID = OD.OrderID;</p>
<p>The above may work, but behind the scenes the database&#8217;s query analyzer has to associate each of the selected columns with their respective tables.  Although this is a fast process, it can be skipped entirely by simply fleshing out the query like this:</p>
<p>SELECT O.OrderID, O.FirstName, O.LastName, OD.ItemName, OD.Quantity<br />
FROM Orders O<br />
INNER JOIN OrderDetails OD<br />
ON O.OrderID = OD.OrderID;</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/06/30/optimize-sql-queries-by-using-aliases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SELECT TOP N in Oracle</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/04/11/select-top-n-in-oracle/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/04/11/select-top-n-in-oracle/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 16:30:54 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[sql server]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=20</guid>
		<description><![CDATA[Being used to SQL Server, I get messed up when moving to Oracle.  For reference, here are equivalent top N queries in both environments:
SQL Server:
SELECT TOP 10 book_name, price*sales
FROM tblBooks;
Oracle:
SELECT book_name, price*sales
FROM tblBooks
WHERE ROWNUM 


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Being used to SQL Server, I get messed up when moving to Oracle.  For reference, here are equivalent top N queries in both environments:</p>
<p><strong>SQL Server</strong>:</p>
<p>SELECT TOP 10 book_name, price*sales<br />
FROM tblBooks;</p>
<p><strong>Oracle</strong>:</p>
<p>SELECT book_name, price*sales<br />
FROM tblBooks<br />
WHERE ROWNUM <= 10;</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/04/11/select-top-n-in-oracle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to SUM Bit Fields in SQL</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/04/06/how-to-sum-bit-fields-in-sql/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/04/06/how-to-sum-bit-fields-in-sql/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 12:49:55 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/2008/04/06/how-to-sum-bit-fields-in-sql/</guid>
		<description><![CDATA[By default, SQL Server doesn&#8217;t allow an operation like this:
SELECT SUM(blnBitColumn) FROM tblTable;
In order to achieve this result, you must first convert the bit column to a numeric type:
SELECT SUM(CONVERT(int,blnBitColumn)) FROM tblTable;
This counts the number of times the bit is true.
If you want to get the flip-side of that to see how many times the [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>By default, SQL Server doesn&#8217;t allow an operation like this:</p>
<p>SELECT SUM(blnBitColumn) FROM tblTable;</p>
<p>In order to achieve this result, you must first convert the bit column to a numeric type:</p>
<p>SELECT SUM(CONVERT(int,blnBitColumn)) FROM tblTable;</p>
<p>This counts the number of times the bit is <strong>true</strong>.</p>
<p>If you want to get the flip-side of that to see how many times the bit is <strong>false</strong>, just subtract the total number of bits from the positive:</p>
<p>SELECT COUNT(blnBitColumn)-SUM(CONVERT(int,blnBitColumn)) FROM tblTable;</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/04/06/how-to-sum-bit-fields-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Connections in ASP.NET &#8211; What you learned is WRONG!</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/02/15/sql-connections-in-aspnet-what-you-learned-is-wrong/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/02/15/sql-connections-in-aspnet-what-you-learned-is-wrong/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 19:22:49 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Blogging]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[interfaces]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/2008/02/15/sql-connections-in-aspnet-what-you-learned-is-wrong/</guid>
		<description><![CDATA[When we learn how to open and use a database connection with ASP.NET, as with any other programming concept in any other programming language, the simplified version used to explain what’s going on is not truly representative of the quality professional code we will one day be expected to write.
Opening and Closing Connections
Case in point: [...]


Related posts:<ol><li><a href='http://www.alwaysgetbetter.com/blog/2009/11/05/vista-fix-sql-server-express-error-create-database-permission-denied-database-master/' rel='bookmark' title='Permanent Link: VISTA: How to fix SQL Server Express Error &#8211; CREATE DATABASE permission denied in database &#8216;master&#8217;'>VISTA: How to fix SQL Server Express Error &#8211; CREATE DATABASE permission denied in database &#8216;master&#8217;</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>When we learn how to open and use a database connection with ASP.NET, as with any other programming concept in any other programming language, the simplified version used to explain what’s going on is not truly representative of the quality professional code we will one day be expected to write.<br />
Opening and Closing Connections</p>
<p>Case in point: managing of sql database connection resources. How many of us learned to write something like this:</p>
<p><code><br />
// Create a new SQL Connection object<br />
SqlConnection conn = new SqlConnection( connectionString );</p>
<p>// Open the connection to the database<br />
conn.Open();</p>
<p>// Create a new SQL Command<br />
SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn );</p>
<p>// Execute the command<br />
cmd.ExecuteNonQuery();</p>
<p>// Close the database connection<br />
conn.Close();<br />
</code></p>
<p>Sure it’s easy to follow, but if you deploy that on a moderately busy server you are going to make your client very unhappy.</p>
<h2>Dispose Resources</h2>
<p>SQLConnection and SQLCommand objects reference unmanaged resources, meaning the C# garbage collector has no framework knowledge about your object. Since these classes both implement the disposable interface it is important to call the Dispose() method in order to correctly free your application’s used memory.</p>
<p>So our code gets updated to look like this:</p>
<p><code><br />
// Create a new SQL Connection object<br />
SqlConnection conn = new SqlConnection( connectionString );</p>
<p>// Open the connection to the database<br />
conn.Open();</p>
<p>// Create a new SQL Command<br />
SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn );</p>
<p>// Execute the command<br />
cmd.ExecuteNonQuery();</p>
<p>// Dispose of the command<br />
cmd.Dispose();</p>
<p>// Close the database connection<br />
conn.Close();</p>
<p>// Dispose of the connection object<br />
conn.Dispose();<br />
</code></p>
<h2>Trap for Errors</h2>
<p>What happens if there’s a problem, and your code fails to complete? If your application crashes before your objects are disposed, you are left with the same effect as if you had never disposed your objects at all!</p>
<p>Fortunately, C# includes the try … finally reserved words. If anything within the try { } block fails, the finally { } still executes. We can easily apply this to our program code:</p>
<p><code><br />
// Create a new SQL Connection object<br />
SqlConnection conn = new SqlConnection( connectionString );</p>
<p>try<br />
{<br />
// Open the connection to the database<br />
conn.Open();</p>
<p>// Create a new SQL Command<br />
SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn );</p>
<p>try<br />
{<br />
// Execute the command<br />
cmd.ExecuteNonQuery();<br />
}<br />
finally<br />
{<br />
// Dispose of the command<br />
cmd.Dispose();<br />
}</p>
<p>// Close the database connection<br />
conn.Close();<br />
}<br />
finally<br />
{<br />
// Dispose of the connection object<br />
conn.Dispose();<br />
}<br />
</code></p>
<p>For my own part, I prefer the using keyword. We can include a using call anywhere we would ordinarily use a disposal object. When the code is compiled, it behaves the same as try … catch, but leaves our program code much more readable.</p>
<p>Even better, we don’t even have to bother calling Dispose() because it does it for us!</p>
<p><code><br />
// Create a new SQL Connection object<br />
using ( SqlConnection conn = new SqlConnection( connectionString ) )<br />
{<br />
// Open the connection to the database<br />
conn.Open();</p>
<p>// Create a new SQL Command<br />
using ( SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn ) )<br />
{<br />
// Execute the command<br />
cmd.ExecuteNonQuery();<br />
}</p>
<p>// Close the database connection<br />
conn.Close();<br />
}<br />
</code></p>
<p>Slick.</p>
<h2>Open Late, Close Early (like a bank)</h2>
<p>There is one more thing I would add to this. Creating objects in memory takes time. Although it happens in fractions of a second too fast to be detectable by us, it’s important not to waste processing time wherever possible.</p>
<p>Whenever we Open() a database connection, we expect to use the database right away. If we then create an SqlCommand, we’re wasting the open connection’s time. It’s as if we pick up the phone and listen to the dial tone while we then flip through the white pages looking for the number we want to call.</p>
<p>Let’s change our example code so we will now Open() at the last possible opportunity, and Close() right away when we’ve made our call:</p>
<p><code><br />
// Create a new SQL Connection object<br />
using ( SqlConnection conn = new SqlConnection( connectionString ) )<br />
{<br />
// Create a new SQL Command<br />
using ( SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn ) )<br />
{<br />
// Open the connection to the database<br />
conn.Open();</p>
<p>// Execute the command<br />
cmd.ExecuteNonQuery();</p>
<p>// Close the connection to the database<br />
conn.Close();<br />
}<br />
}<br />
</code></p>
<p>In the end, the program code we wrote is very similar to the newbie code we started with. However, we’re now protecting our system from memory leaks, and we’re protecting our database from wasted clock cycles. Our code is easy to read and stable.</p>


<p>Related posts:<ol><li><a href='http://www.alwaysgetbetter.com/blog/2009/11/05/vista-fix-sql-server-express-error-create-database-permission-denied-database-master/' rel='bookmark' title='Permanent Link: VISTA: How to fix SQL Server Express Error &#8211; CREATE DATABASE permission denied in database &#8216;master&#8217;'>VISTA: How to fix SQL Server Express Error &#8211; CREATE DATABASE permission denied in database &#8216;master&#8217;</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/02/15/sql-connections-in-aspnet-what-you-learned-is-wrong/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Create Full Text Search Using mySQL</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/02/14/how-to-create-full-text-search-using-mysql/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/02/14/how-to-create-full-text-search-using-mysql/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 14:40:04 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[e-commerce]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/2008/02/14/how-to-create-full-text-search-using-mysql/</guid>
		<description><![CDATA[Search is one of the most basic features visitors expect when they come to a web site.  This is especially true in e-commerce where your ability to make a sale is directly related to your customer’s ability to find the product they’re looking for.
Using Third-Party Solutions
Many first-time site owner choose to go with Google [...]


Related posts:<ol><li><a href='http://www.alwaysgetbetter.com/blog/2009/11/05/vista-fix-sql-server-express-error-create-database-permission-denied-database-master/' rel='bookmark' title='Permanent Link: VISTA: How to fix SQL Server Express Error &#8211; CREATE DATABASE permission denied in database &#8216;master&#8217;'>VISTA: How to fix SQL Server Express Error &#8211; CREATE DATABASE permission denied in database &#8216;master&#8217;</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Search is one of the most basic features visitors expect when they come to a web site.  This is especially true in e-commerce where your ability to make a sale is directly related to your customer’s ability to find the product they’re looking for.</p>
<h2>Using Third-Party Solutions</h2>
<p>Many first-time site owner choose to go with Google Custom Search because of its easy setup and because of Google’s incredible indexing reach and results.  I don’t like the standard edition of the Custom Search because of the branding – your search results advertise Google and provide links to competing content.  For an e-commerce site to lose control of such a critical function, the results can be costly.</p>
<h2>Don’t Give Up Control</h2>
<p>Especially in e-commerce, it is best to never give up control over any content.  Advanced users may choose to ignore your site’s search tool and use Google to get at your content anyway (via the site: directive) but in the general case there is great potential to keep selling useful products to your potential customers even while they search your site for other items.</p>
<p>Incorporating a decent search tool into a web site using PHP is dead simple.  All it involves is a database table with 3 or more rows and a little bit of an eye for layout.  Even if you don’t consider yourself a designer, having a look around other search engines will give you a feel for how results should display.</p>
<h2>Creating the Search Tables</h2>
<p>Let’s get started.  Our simplistic database table (PRODUCTS) will consist of the following columns:</p>
<table>
<tr>
<th>Column Name</th>
<th>Data Type</th>
<th>Description</th>
</tr>
<tr>
<td>intID</td>
<td>int</td>
<td>Product ID and Primary Key</td>
</tr>
<tr>
<td>vcrName</td>
<td>varchar(25)</td>
<td>Product Name</td>
</tr>
<tr>
<td>txtDescription</td>
<td>text</td>
<td>Product Description</td>
</tr>
<tr>
<td>vcrPhoto</td>
<td>varchar(40)</td>
<td>Path (URL) to product photo</td>
</tr>
</table>
<p>Obviously this is just a simplified example, but the product ID, name, description and photo should be enough for the purposes of our demonstration.</p>
<p>The SQL to create the table looks like this:</p>
<p>CREATE TABLE PRODUCTS<br />
(<br />
intID int auto_increment,<br />
vcrName varchar(25),<br />
txtDescription text,<br />
vcrPhoto varchar(40),<br />
CONSTRAINT PRODUCTS_pk PRIMARY KEY ( intID )<br />
);</p>
<h2>Add the Full-Text Index</h2>
<p>In this example, we’re interested in searching the name and description fields of our products.  In order to add the full-text index to our table, we use the ALTER TABLE command:</p>
<p>ALTER TABLE PRODUCTS ADD FULLTEXT( vcrName, txtDescription );</p>
<p>Alternatively, we could have created the index along with the table in our original CREATE statement like this:</p>
<p>CREATE TABLE PRODUCTS<br />
(<br />
intID int auto_increment,<br />
vcrName varchar(25),<br />
txtDescription text,<br />
vcrPhoto varchar(40),<br />
CONSTRAINT PRODUCTS_pk PRIMARY KEY ( intID ),<br />
FULLTEXT( vcrName, txtDescription )<br />
);</p>
<h2>Searching For Text</h2>
<p>Now that the index has been created, we can go ahead and search the database.  To activate full-text search, we use the MATCH () AGAINST () syntax like this:</p>
<p>SELECT intID, vcrName, txtDescription, vcrPhoto<br />
FROM PRODUCTS<br />
WHERE MATCH( vcrName, txtDescription ) AGAINST ( ‘search terms here’ );</p>
<p>That’s all there is to it!  Anyone with access to a mySQL database should be able to incorporate search into their sites without too much difficulty.  Of course this is a very basic introduction, but should be more the sufficient to get going with.</p>


<p>Related posts:<ol><li><a href='http://www.alwaysgetbetter.com/blog/2009/11/05/vista-fix-sql-server-express-error-create-database-permission-denied-database-master/' rel='bookmark' title='Permanent Link: VISTA: How to fix SQL Server Express Error &#8211; CREATE DATABASE permission denied in database &#8216;master&#8217;'>VISTA: How to fix SQL Server Express Error &#8211; CREATE DATABASE permission denied in database &#8216;master&#8217;</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/02/14/how-to-create-full-text-search-using-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
