THE SQL Server Blog Spot on the Web

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

Alexander Kuznetsov

A foolish consistency is the hobgoblin of little minds?

In the following script one out of six semicolons does not parse. Can you guess which one we need to remove without running it or scrolling down for the answer? Does it make sense to you?

 

BEGIN TRY ;
    
PRINT 'Inside TRY' ;
END TRY ;
BEGIN CATCH ;
    
PRINT 'Inside CATCH' ;
END CATCH ;
  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 BEGIN TRY ;
    
PRINT 'Inside TRY' ;
-- the wrong semicolon commented    
END TRY --;
BEGIN CATCH ;
    
PRINT 'Inside CATCH' ;
END CATCH ;
 

Published Tuesday, February 16, 2010 2:02 PM by Alexander Kuznetsov

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

 

Atul said:

hmm, not justifiable.

February 17, 2010 12:27 AM
 

AaronBertrand said:

Alex,

I knew the answer but only because I saw this Connect item recently.  Lame that it has been closed as "won't fix" and/or "by design" from MSFT:

http://connect.microsoft.com/SQLServer/feedback/details/498085

February 17, 2010 2:27 AM
 

Alexander Kuznetsov said:

Aaron, if this is "by design", it would be nice to learn what kind of logic, which chain of reasoning leads to design like this.

February 17, 2010 9:22 AM
 

Paul Nielsen said:

I think of "End Try Begin Catch" as a single command.

February 17, 2010 3:08 PM
 

Alexander Kuznetsov said:

Paul, if this is the case, then maybe END ELSE BEGIN should be a single command too. But this is not the case, the following parsed:

IF 1 > 0

   BEGIN ;

       PRINT '1>0' ;

   END ;

   ELSE

   BEGIN ;

       PRINT '1<=0' ;

   END ;  

February 17, 2010 3:16 PM
 

devio said:

well, you cannot write

IF (cond) ;

END ELSE; BEGIN

either (at least in 2005)

February 21, 2010 6:59 AM
 

FindTim said:

I know this is an old thread - but Paul's comment of thinking about the end try begin catch as a single command hits the nail on the head.

You cannot have a try without a catch.  But You can have a If without and Else

You can write:

begin try;

select 1;

end try begin catch;

print '1'

end catch;

but you cannot write:

begin try;

select 1;

end try

PRINT '2'

begin catch;

print '1'

end catch;

Nothing is allowed between the end try and begin catch.

Now to your point and devio's there is no END ELSE; in T-SQL.  However, the Begin End construct lives outside of just IF and so you can have a

Begin

End;

put stuff here and then write and additional

Begin

End;

When I first saw Try Catch logic in T-SQL I would agree with you Alex, this issue seems illogical, but not anymore.

July 2, 2010 5:43 PM
 

Alexander Kuznetsov said:

FindTim,

Yes, I agree that Paul Nielsen's explanation does make sense.

However, this concept of "End Try Begin Catch" as a single command needs to be learned, and here is the question: is this concept absolutely necessary? Does it add any value to the product? Can we live without it? Can't we just allow a semicolon in one more place and have a simpler language with same features and less rules?

You know, this seems to be a perfect case to apply Occum's razor and eliminate yet another unnecessary complication.

July 2, 2010 6:01 PM
 

FindTim said:

I like your drive that cuts to the quick.  Do we really need this?  This is a great question.

There is a lot of bloat in the language.  We probably should switch to a cleaner statement

Try

Catch

End TryCatch;

I think this highlights a big problem with SQL Server.  We have lost some big names in the last few years (Jim and Ken and now Mosha going to Bing) others I am sure whose names I don't know.  Who is going to be the champion driving the product and languages.  Unfortunately things have been implemented haphazardly.  So this is what we've got.

Maybe you can move to Redmond and straighten things out. :)

July 2, 2010 6:49 PM

Leave a Comment

(required) 
(required) 
Submit

About Alexander Kuznetsov

Alex Kuznetsov has been working with object oriented languages, mostly C# and C++, as well as with databases for more than a decade. He has worked with Sybase, SQL Server, Oracle and DB2. He regularly blogs on sqlblog.com, mostly about database unit testing, defensive programming, and query optimization. Alex has written a book entitled "Defensive Database Programming with Transact-SQL" and several articles on simple-talk.com and devx.com. Currently he works at DRW Trading Group in Chicago, where he leads a team of developers, practicing agile development, defensive programming, TDD, and database unit testing.

This Blog

Syndication

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