Tuesday 12 May 2009

The joys of debugging and perfectible documentation - part 1

SQL Server (I am referring to SQL2k, from SQL 2005 on there is better error handling, though the problem remains basically the same) has a simple error trapping functionality.


(do something possibly wrong)
IF @@ERROR <> 0
BEGIN
(do something)
END

e.g.

SET @VAL  = CAST( @string AS FLOAT)
IF @@ERROR <> 0
BEGIN
SET @VAL = 0
END

It generally works fine BUT if you use such a code in UDF -  user defined function the code compiles just fine, but it just doesn't work. The exception is never handled and the misuse of @@ERROR is not signalled. This, as the microsofties say, is 'by design'. Too bad that the @@ERROR Books Online entry doesn't mention this fact, I had to waste a (wee) bit more time and confirm this information in somebody else's blog.

No comments:

Post a Comment