Reputation: 399
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.
Upvotes: 0
Views: 879
Reputation: 89
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
Upvotes: 1
Reputation: 272
declare @sql varchar(100) = 'Select 1/0'
Begin Try
exec(@sql)
End Try
Begin Catch
print 'error='+ERROR_MESSAGE()
return
End Catch
Upvotes: 1