SQL Formatting Trick

I learned a very useful T-SQL script formatting trick today. I often talk about formatting and commenting for the “person from mars” which is usually “you” coming back to look at your code 3 years from now having forgotten everything you ever knew about this project and learned better ways to do everything that was done in this project but now you just need to make a “quick” update. How do you find the one piece of code in the thousands of lines of code (perhaps millions) that make up this solution? Well hopefully you left yourself a few clues AKA comments that call out important lines or sections of code and hopefully the code has been formatted in a way that makes it easy to read and debug. Well developers cannot survive on google searches and hope alone! We need to help our future selves out a little. So let’s take a look at a formatting trick that will make your T-SQL statements easier to read and debug. When creating stored procedures, views and user defined functions you may need to comment out one or more lines for debugging or testing reasons, however when all of your code is on a single line it makes it very difficult to comment out a single column reference or where clause. So while debugging stored procedures I used to spend a lot of time (before I learned this new trick) using a keyboard shortcut. The keyboard short I had been using was “Ctrl + Right Arrow” this keyboard short cut skips to the beginning of the next word (commas count as words so it will stop at every comma as well) so in a statement such as: “Select FirstName, LastName, Phone, EmailAddress from Person.Person” the keyboard shortcut will move us from the beginning of the word Select to the beginning of the word FirstName the next time we press the arrow key we will move from the begging of FirstName to just before the comma (after FirstName). Using this short cut the key patter would be Ctrl + Right Arrow 2x (press the right arrow twice while holding down the Ctrl key) then Press Enter (to move “, FirstName” to the next line) then Ctrl + Right Arrow 2x, Enter (to move “, LastName” to the next line). Repeat for each column in the table. This works and I’ve been using it for years but there is a better way! The New Way… Using Search and Replace we can replace all commas with a carriage return and a comma.
Ctrl + H opens the search and replace window.
In the find what box type a comma.
In the Replace with box type \n,
then in Find Options select the “Use:” checkbox
then choose Regular Expressions from the dropdown.
This will replace all commas with a carriage return and a comma.
Converts this: Select FirstName, LastName, Phone, EmailAddress from Person.Person
To this: SelectFirstName, LastName, Phone, EmailAddressfrom Person.Person

Thanks for the great tip, Cal!

What is SQL Server

If you are reading this post you have of course heard the term DataBase… Depending who you are and what you do for a living that term may or may not mean anything to you. If you are a DBA (DataBase Administrator) look away! This post is not for you. lol This post is intended for those who have no idea what a database is or what Microsoft SQL Server is or why they should care.
OK so now that we’ve got that out of the way on with the definition.

What is SQL Server? 
SQL Server is Microsoft’s RDBMS (Relational DataBase Management System). Like its competitor Oracle, Microsoft’s SQL Server is the database applications that a website or other application might use to store its list of Customers, Orders, Products and Employees. If you have booked a flight reservation on line (or by phone) or you have a Costco or Netflix member ship then you have no doubt been exposed to a database and perhaps even had your purchasing habits stored in the database and used to market products to you that the vendor knows you want. Put simply Microsoft SQL Server is a DataBase product used to store the data collected and used by a variety of computer software applications. It is a giant electronic file cabinet for your computer based data.
These days most organizations (Microsoft shops) are running Microsoft SQL Server 2005, Microsoft SQL Server 2008 or Microsoft SQL Server 2008R2. You can install Microsoft SQL Server 2008R2 software on a server in your office or you can run SQL Server in the Cloud! For more information about Microsoft SQL Server in the cloud see the following article: Introduction to the Cloud and Windows Azure

SQL Server Bulk Updates and Locking

When doing a large update of a table in a SQL Server database you may gain some performance by locking at the Table level instead of allowing SQL Server to do Page or Row level locking.
This can be accomplished using Table Hints. Table hints override the query optimizer default plan for the executed query. Table hints apply to data manipulation language (DML) statements such as Select, Insert, Update and Delete. Table hints should be specified in the FROM clause. Table hints allow the user to choose a specific locking method, indexes to use, or other options for DML statements.
It should be noted however that you are second guessing the query optimizer and if conditions change in the database that might make the query work better SQL Server may still use your less efficient table hint.
The ProDataMan way
If you use Optimizer Hints AKA Table Hints make sure to document any scripts or procedures that use table hints so they can be reviewed when changes to the database schema are made that might affect the Table Hint.

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

This is an error that you get when you try to view the built-in reports in SQL Server 2008 when the database compatibility is set to anything less than 2005(90).
To fix this and make the built-in reports work:
Right click on the database
Select properties
On the options tab change the compatibility level to SQL 2005 or later.