SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Formatting and Readability

By Oliver Gaudreault,

Today we have a guest editorial from Oliver Gaudreault.

Too often I have seen stored procedures, functions and general SQL scripts that are indecipherable. What does this mean? It means that you will be forced to spend time making the code readable if you are required to debug or investigate code. Many times before I have run across someone who edited an IF statement thinking it was in a block but wasn't. The code in the IF statement applied to everything and the result was not what the original business logic intended. Proper structure and form of the code would remedy this problem.

Using the following would help to make the code more readable:

  • Ensuring all flow control statements start and terminate appropriately.
  • Indentation is properly structured.
  • Ensure that all flow control statements (such as IF, WHILE, etc) terminate appropriately. For flow control statements that are inline you don't necessarily need a BEGIN and END to the flow control statement. However in my experience I believe it is necessary.

Not only does this methodology apply to SQL but I believe it also applies for any modern programming language. Some may think that by making your code readable that it might render you replaceable. I beg to differ! I want to see people programming who are versatile. Programmers, who can quickly and easily find and repair an issue with little wasted effort. Lack of time should not equal bad practices. Good practises should equal more time.

This is, of course debatable and I would like to know others thoughts on this?

Total article views: 223 | Views in the last 30 days: 1
Related Articles

A Security Control Inconsistently Applied Is Not a Control

Some things and readings today reminded me of this: a security control inconsistently applied is not...


Update Statement with an outer apply

update statement using an outer apply in the qualifier


Applying TRANSACTION for DDL statements

Applying TRANSACTION for DDL statements


Write Cleaner T-SQL Using Version Control

If we only use version control as a way to back up our code then it is pure overhead but actually th...


Querying Microsoft SQL Server : Control Statements in T-SQL

Querying Microsoft SQL Server : Control Statements in T-SQL: Control statements are used to control ...