Home Why does T-SQL not stop on errors
Reply: 0

Why does T-SQL not stop on errors

user1011
1#
user1011 Published in June 19, 2018, 6:17 pm

If I do the following query

EXEC spFoo
PRINT 'TEST'

and spFoo throws an error, it still executes the print statement.

However if I do

BEGIN TRY
    EXEC cdb.spFoo
    PRINT 'TEST'
END TRY
BEGIN CATCH
    THROW;
END CATCH

It behaves as I expected and does not continue after an error.

Could someone please explain this behaviour for me? It still continues on even if I encapsulate it in a transaction. It is not just the with a print-statement but also with any other thing. My initial thought was that it was a severity problem but the severity was level 16. Is this normal T-SQL behaviour? If so, what motivates this design that contradicts every other language I have ever worked with that directly escalates the error?

I have tried and seen the same behaviour in SQL Server 2012, 2014 and 2017 across several different machines. The stored procedure in question is linked to a SQL CLR.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311675 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO