Letting the database to the heavy lifting: Capitalization via SQL

Years ago I had the pleasure of meeting Ben Forta and listen to his thoughts on web development. One thing that stuck with me was his statement that:

“Dynamic programming languages like PHP and Coldfusion are great, but developers need to leverage the database more. Allow the database to do it’s job, the heavy lifting”

With that being said, I was recently working with a database that contained users first names. The data was not snantized at entry, and contained a mix of lower cased (ex. josh) and upper cased (ex. Josh) first characters in the firstname column. I needed to always present the users first name in a capitalized format.

The first solution that most developers would reach for is dynamically parsing the users first name, then upper-casing the first character. This works, but in my opinion isn’t the most elegant solution.

With a simple SQL statement, like the one below, we can return the users first name already in a capitalized state.

SELECT (UPPER(LEFT(firstname,1))+SUBSTRING(firstname,2,LEN(firstname))) as firstname 
FROM users
It’s quick to implement, its easy to work with, and it’s also very fast on the performance scale.
Yes, I understand that there are cool convenience functions in like ucfirst() in PHP, but I’m a lazy programmer and I don’t want to have to remember to use ucfirst() every time I’m dealing with the users name. I’d rather get the information in the correct format the first time, then remembering to groom it every time before I use it. Set it and forget it.

SQL Server 2008: FIXED “Saving changes is not permitted”

Working in SQL Server 2008 I received this confusing error when editing the structure of a table:

“Saving changes is not permitted. The changes that you have made require the following tables to be dropped and re-created. You have either made changes to a table that can’t be re-created or enabled the option Prevent saving changes that require the table to be re-created.”

I tried many things but I kept getting this error. After some trouble shooting I found the following information:

This error happens because “Saving Changes is not permitted” when using SQL Server 2008 Management Studio to modify the structure of a table especially when SQL Server needs to drop and recreate a table to save the structural changes. It is always advised to make a structural change for a table using TSQL. However, it is a convenient option for database developers to use SQL Server Management Studio make such changes  as the  Prevent Saving Changes That Require Table Re-creation option is enabled by default in SQL Server 2008 Management Studio.

I can understand why this feature would be helpful to have but to have it enabled by default is just silly.

I found that you can disable this feature by:

  1. Open the Tools menu then click on Options
  2. Open the “Designers” tree
  3. Click on “Table and Database Designers”
  4. Uncheck “Prevent saving changes that require table-creation”
  5. Press “Ok”

Once I disabled the “Prevent saving changes that require the table re-creation” option,  I was ahead and save the changes I needed to make.

SQL Server 2008: FIXED “Data has changed since the Results pane was last retrieved”

Today I was trying to edit the value in a table cell through the query browser in Microsoft SQL Server Management Itudio and I kept getting the following error:

Data has changed since the Results pane was last retrieved. Do you want to save your changes now?
(Optimistic Concurrency Control Error)
Click Yes to commit your changes to database anyway.
Click No to discard your change and retrieve the current data for this row.
Click Cancel to continue editing.

I would click “Yes”, and get another error:

No row was updated.
The data in row X was not committed.
Error Source: Microsoft.VisualStudio.DataTools.
Error Message: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows(N rows).
Correct the errors and retry or press ESC to cancel the change(s).

I was killing myself trying to figure out what was wrong. I was the only person working on the database, so the data wasnt being changed behind my back.

After an hour on goolge I came across a Microsoft tech article for MS SQL 2005, http://support.microsoft.com/kb/925719

It stated the issue could occure from one of the following conditions:

  • The table contains one or more columns of the text or ntext data type.
  • The value of one of these columns contains the following characters:
  • Percent sign (%)
  • Underscore (_)
  • Left bracket ([)
  • The table does not contain a primary key.

I double checked the table I was working on and realized it didnt have a primary key. It must have gotten dropped in the data migration from MS SQL 2000 to MS SQL 2008. A simple right click, set primary key and my problem was fixed.

Verify the primary keys! It’s a simple fix to a head breaker of a problem. I hope someone finds this and it fixes their issue.