0
Begin Try
exec @sql
End Try

Begin Catch
Display error message (How to?)
return
End Catch

Now, on error with the exec @sql statement, I want it to display the error message first and then end the code.

2 Answers 2

1
declare @sql varchar(100) = 'Select 1/0'  
Begin Try  
exec(@sql) 
End Try  

Begin Catch
print 'error='+ERROR_MESSAGE()
return
End Catch

Sign up to request clarification or add additional context in comments.

Comments

1

To get the error message generated by system

  declare @sql varchar(100) = 'Select 1/0'  
  Begin Try  
        exec(@sql) 
  End Try  
  Begin Catch
        print ERROR_MESSAGE()
  return

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.