THE SQL Server Blog Spot on the Web

Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | Join | Help
in Search

Aaron Bertrand

Ladies and gentlemen, start your semi-colons!

Almost a year ago now, I wrote up a blog post detailing the things that I strive to do when writing T-SQL modules (mostly stored procedures).  One of the things that I have been doing since my very first common table expression (CTE) back in the Yukon beta is ending all relevant statements with statement terminators (semi-colons).  A few responses to the above post indicated that they don't currently use statement terminators, and had no interest in starting.  Let's quickly recap the reasons why I started making this a habit.
 

Today, I use CTEs and Service Broker.

Unless it is the first statement in a batch, the previous statement prior to a CTE declaration needs to be terminated with a semi-colon.  This is also true for Service Broker commands (e.g. SEND ON CONVERSATION).  Some people "fix" this by simply writing code ike this:

;WITH foo AS (...) ...

And typically when I post CTE examples to newsgroups / forums, I do this, to avoid the inevitable comeback with this error message attached (because you never know where they are going to jam your example, and then complain that it didn't work):

.Net SqlClient Data Provider: Msg 319, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'with'. If this statement is a common table expression
or an xmlnamespaces clause, the previous statement must be terminated with a semicolon

But I think that syntax is far too ugly and counter-intuitive for my production code (YMMV).  So for this reason, all relevant statements in my code get a semi-colon.  I didn't go to the extent of inspecting all of my existing code and adding them, but if I was re-visiting a module for some other enhancement, I would take the few seconds and add any semi-colons that were missing.  I didn't feel like I needed to do this, but thought it best to future-proof my code as much as possible, when convenient.  And of course when writing new code, I would always terminate.  This is probably best demonstrated by scrolling through my blog posts over the past year or two, and trying to spot any examples where I neglected to add a semi-colon.  I doubt you'll find many!
 

Someday, I may use MERGE.

In SQL Server 2008, a similar requirement was added for MERGE: you can't run a MERGE statement unless it is properly terminated with a semi-colon.  Otherwise, you get this error message:

.Net SqlClient Data Provider: Msg 10713, Level 15, State 1, Line 21
A MERGE statement must be terminated by a semi-colon (;)

So because of this, the set of reasons for future-proofing your code in this way has doubled: while your CTEs and MERGE statements obviously work today, because your procedure compiled, a user modifying them later might break them, and this will cause an extra debugging step.  So at this point, I was debating whether it might make sense to comb through all of my code and add semi-colons where appropriate, but was still sitting quite squarely on the fence.
 

Not convinced?  Hold on, there's more!

In the previous post, I suggested that someday Microsoft will make statement terminators mandatory.  Well, they have.  Statements that DON'T end with a proper statement terminator are on the deprecation list, which means that at some point, any statement that doesn't end with a semi-colon will cause an error similar to those listed above.  Sounds funny, but it is true.  If you don't believe me, please review these topics in Books Online:

Transact-SQL Syntax Conventions (Transact-SQL)
http://msdn.microsoft.com/en-us/library/ms177563.aspx
(See the second-last row in the first table.)

Deprecated Database Engine Features in SQL Server 2008
http://msdn.microsoft.com/en-us/library/ms143729.aspx
(It's a lot of scrolling; search the page for "semicolon.")

I'm not sure if they're doing this because the ANSI standard now declares statement terminators as mandatory, or because as the T-SQL language gets more and more complex, the complexities in the parser code multiply, or a combination of both.  But I am no longer on the fence: it is on my "long-term task list" to systematically attack chunks of T-SQL modules and fix these pesky situations where statement terminators are missing.
 

Conclusion

I've outlined several good reasons to get into the habit of ending all of your T-SQL statements with semi-colons, at least for work going forward.  And I may have even painted a picture that shows you it may worthwhile to go back and "fix" your existing codebase -- maybe coupled with a simultaneous code review of some other kind, to help justify the effort.  Because "a future version of SQL Server" might be closer than you think.

[Hmm, now I'm thinking the title of this post sounds a little dirty.]

 


Published Thursday, September 03, 2009 2:13 PM by AaronBertrand

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Jonathan Kehayias said:

Very nice, especially since the competing RDMBS systems (at least Oracle) has required this for a while now.  I am pretty good about using terminators in my code because of time working with Oracle over the last two years.

September 3, 2009 2:44 PM
 

Whitney Weaver said:

Aaron,

I've been wondering when this would become official.  I have had a couple MS people mention this in the past but always in terms of "some day".  

Personally, I always use them and try to get others to do so if working on the same project with me.  Now I have another place to point them, thanks for that.

Hadn't considered the dirty title angle.  But thanks for kicking off the parade of jokes going through my head now.

September 3, 2009 2:49 PM
 

Brian Tkatch said:

"But I think that syntax is far too ugly and counter-intuitive for my production code (YMMV). "

Exactly!

I used to Oracle, so the lack of semi-colons looks funny to me. Though, i still wan to be able to highligh half a query and run it, semi-colon or not!

September 3, 2009 3:25 PM
 

AaronBertrand said:

Brian, that's an interesting point, I hadn't thought about the partial ad-hoc query.  I wonder if the enforcement could only kick in when the batch has more than one statement (but then part of the point of the semi-colon is so that the parser can identify individual statements, so this is kind of chicken and egg).  Or if you could intentionally run SSMS in an enforcement-bypass mode (kind of like a debug mode).

September 3, 2009 3:32 PM
 

SqlServerKudos said:

Kudos for a great Sql Server article - Trackback from SqlServerKudos

September 3, 2009 4:14 PM
 

jamiet said:

There is an ever-so-slightly-related other reason to use semicolons.

In Oracle tools it is general convention to be able to position your cursor within a statement, hit F9, and only that statement will execute.

There is a growing body of people who want similar in SSMS (there's a Connect submission for it somewhere but I couldn't find it cos Connect is playing up. Again.) and one can assume that this may rely on the use of semicolons for termination. So, in anticipation of them MAYBE introducing this very useful feature in the next version - use semicolons!!!

-Jamie

September 3, 2009 5:51 PM
 

Wiseman82 said:

Thanks for the heads up!  I've been using semi-colons a lot more since the introduction of SQL 2005 (mainly because they are required for CTEs), but I can't say that I use them religiously. If it's deprecated, we might as well all get into the habit of using them now and also to make our lives easier when we upgrade to the next version of SQL Server (or whatever version they finally remove it from).

Thanks. :-)

September 4, 2009 3:24 AM
 

Jason Haley said:

Interesting Finds: September 4, 2009

September 4, 2009 7:46 AM
 

GlennAlanBerry said:

I agree that it is a good idea to terminate statements with semicolons, since it will finally be required in a future version (I'm guessing SQL 12) of SQL Server.

I make my students do it in my T-SQL classes, so they are used to it at least.

September 4, 2009 11:33 AM
 

gcardoso said:

Very nice post!

I don`t have the habit of use semicolon terminator in my T-SQL code, but now i will adopt.  Who know, the Microsoft implement semicolon terminator as a mandatory syntax in the version of SQL 2011, isn`t?

gcardoso

September 4, 2009 1:19 PM
 

AaronBertrand said:

Oh, I really doubt they will make this mandatory for SQL11.  I think it will take several versions to phase this in gradually.

September 4, 2009 1:23 PM
 

JohnG said:

I've been coding SQL Server T-SQL this way for years.  Having started with IBM's DB2 back in the early 80s and having coded with Oracle from V5 through 10g, which all require semicolons, it was a no-brainer.  It also made the porting of code from SQL Server to Oracle a lot easier.

SQL Server has been the "odd" animal for too long.

September 9, 2009 2:47 PM
 

Aaron Bertrand said:

A few weeks ago, I wrote a post about forming a new habit: always terminate statements with semi-colons

October 6, 2009 11:21 AM
 

Helping people kick bad SQL Server habits « OTO One to One Interactive said:

October 10, 2009 2:28 PM
 

Aaron Bertrand said:

I've been fairly distracted this past week with my " Bad habits to kick " series, so apologies for being

October 17, 2009 2:13 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About AaronBertrand

...about me...

This Blog

Syndication

Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement